// JavaScript Document

jQuery(function() {
	// preload images
	var preloadImages = new Array(
		'images/mousedown_left_bg.png',
		'images/mousedown_right_bg.png',
		'images/mousedown_bg.png'
	);
	for ( var i = 0; i < preloadImages.length; i++) {
	 jQuery('<img>').attr('src', preloadImages[i]);
	}
	// set the menu bg
	var clicked = 0;
	var sub_clicked = 0;	
	var locArray = location.href.split('#');
	locArray = locArray[0].split('/');
	var current = locArray[locArray.length - 1];
	if (current == '') {
		current = 'home-index.html';
	}
	jQuery('li.menu').each(function() {
		if (jQuery('a', jQuery(this)).attr('href') == current) {
			if (jQuery('li.menu').index(this) + 1 == 1) {
				jQuery(this).css('background', 'url(images/mousedown_left_bg.png) no-repeat');
			} else if (jQuery('li.menu').index(this) + 1 == jQuery('.menu').size()) {
				jQuery(this).css('background', 'url(images/mousedown_right_bg.png) no-repeat');
			} else {
				jQuery(this).css('background', 'url(images/mousedown_bg.png) no-repeat');				
			}
			clicked = jQuery('li.menu').index(this) + 1;
		} else {
			jQuery(this).find('li').each(function() {
				if (jQuery('a', jQuery(this)).attr('href') == current) {
					jQuery(this).parents().each(function() {
						var className = this.className.split(' '); 
						if (className[0] == 'menu') {						
							if (jQuery('li.menu').index(this) + 1 == 1) {
								jQuery(this).css('background', 'url(images/mousedown_left_bg.png) no-repeat');
							} else if (jQuery('li.menu').index(this) + 1 == jQuery('.menu').size()) {
								jQuery(this).css('background', 'url(images/mousedown_right_bg.png) no-repeat');
							} else {
								jQuery(this).css('background', 'url(images/mousedown_bg.png) no-repeat');
							}
							clicked =  jQuery('li.menu').index(this) + 1;
						}					
					});
					jQuery(this).css('background', '#107c0b');
					sub_clicked = jQuery('li', jQuery('li.menu')).index(this) + 1;
				}
			});
		}
	});	
	// add listener
	jQuery('li.menu').bind('mouseover', function() {		
		var left = 'url(images/mousedown_left_bg.png) no-repeat';
		var right = 'url(images/mousedown_right_bg.png) no-repeat';
		var normal = 'url(images/mousedown_bg.png) no-repeat';
		if (jQuery('li.menu').index(this) + 1 == 1) {
			jQuery(this).css('background', left);
		} else if (jQuery('li.menu').index(this) + 1 == jQuery('li.menu').size()) {
			jQuery(this).css('background', right);
		} else {
			jQuery(this).css('background', normal);
		}
		jQuery(this).css('cursor', 'pointer');
	});		
	jQuery('li.menu').bind('mouseout', function() {
		if (jQuery('li.menu').index(this) + 1 != clicked) {
			jQuery(this).css('background', '');
		}
	});
	jQuery('li.menu').bind('click', function() {
		window.location.href = jQuery('a', jQuery(this)).attr('href');
	});
	jQuery('li', jQuery('li.menu')).bind('mouseover', function() {	
		jQuery(this).css('background', '#107c0b');		
	});
	jQuery('li', jQuery('li.menu')).bind('mouseout', function() {
		if (jQuery('li', jQuery('li.menu')).index(this) + 1 != sub_clicked) {
			jQuery(this).css('background', '#58b155');
		}
	});
	jQuery('li', jQuery('li.menu')).bind('click', function() {
		jQuery('li.menu').unbind('click');
		window.location.href = jQuery('a', jQuery(this)).attr('href');
	});	
	jQuery('img#btn_submit').bind('click', function() {	
		jQuery.ajax({
			type: 'POST',
			url: 'home-sendMsg.html',
			data: 'name=' + jQuery('#name').attr('value') + '&email=' + jQuery('#email').attr('value') + '&phone=' + jQuery('#phone').attr('value') + '&message=' + jQuery('#message').attr('value'),
			success: function(status) {		    	
		    	jQuery('.feedback-panel').animate({left:'-' + (feedbackTab.containerWidth - feedbackTab.tabWidth)}, feedbackTab.speed).removeClass('open');
		    	jQuery('#feedback-panel-form').each(function() {this.reset();});
		    	if (Boolean(status)) {
		    		alert('Thank you for contacting us. We will get back to you as soon as we can!');
		    	} else {
		    		alert('Send failed!');
		    	}
			},
			
		});
	});
	jQuery('img#btn_reset').bind('click', function() {	
		jQuery('#feedback-panel-form').each(function() {this.reset();});
	});
	// contact box
    var feedbackTab = {       
        speed:300,
        containerWidth:jQuery('.feedback-panel').outerWidth(),
        containerHeight:jQuery('.feedback-panel').outerHeight(),
        tabWidth:jQuery('.feedback-tab').outerWidth(),       
        init:function(){
    		jQuery('.feedback-panel').css('height', feedbackTab.containerHeight + 'px');
    		jQuery('.feedback-panel').css('top', (self.innerHeight - feedbackTab.containerHeight) / 2 + 'px'); 
    		jQuery('a.feedback-tab').click(function(event){
                if (jQuery('.feedback-panel').hasClass('open')) {
                	jQuery('.feedback-panel').animate({left:'-' + (feedbackTab.containerWidth - feedbackTab.tabWidth)}, feedbackTab.speed).removeClass('open');
                } else {
                	jQuery('.feedback-panel').animate({left:'0'},  feedbackTab.speed).addClass('open');
                }
                event.preventDefault();
            });
        }
    };  
    feedbackTab.init();
});

// google
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try{ var pageTracker = _gat._getTracker("UA-9134121-5"); pageTracker._trackPageview(); } catch(err) {}
