var no_sib = false;
var system_url = "http://www.thesolarspecialists.com";
var image_dir = system_url + '/dev/wp-content/themes/solar_specialists/images/fancyzoom';
//$.easing.def = 'easeOutBack';

var GLOBALS = {
	load_height: 0,
	ready_height: 0
}

$(document).ready(function() {


	
	$('#contact_head_wrapper').hide();
	$('#contact_head #map ul li.mask').hide();
	
	$('a.contact_us, a.contact_us_side').unbind('click');
	$('a.contact_us, a.contact_us_side').click(function() {
		if( $('#contact_head_wrapper').css('display') == 'none' ) {
			$('#contact_head_wrapper').slideDown();
			$('#get_quote').animate({
				top: "+=300"
			}, 'normal');
			$('html, body').animate({scrollTop:0}, 'fast');
		} else {
			$('#contact_head_wrapper').slideUp();
			$('#get_quote').animate({
				top: "-=300"
			}, 'normal');
			$('html, body').animate({scrollTop:0}, 'fast');
		}
		
		
		
	});
	
	$('#site-footer-links a').each( function() {
		var link_text = $(this).attr('href');
//		alert(link_text);
		if( link_text.indexOf('contact') >= 0 ) {
			$(this).click(function(e) {
				e.preventDefault();
				if( $('#contact_head_wrapper').css('display') == 'none' ) {
				
					$('#contact_head_wrapper').slideDown();
					$('#get_quote').animate({
						top: "+=300"
					}, 'normal');
					$('html, body').animate({scrollTop:0}, 'fast');
				} else {
//					$('#contact_head_wrapper').slideUp();
//					$('#get_quote').animate({
//						top: "-=300"
//					}, 'normal');
					$('html, body').animate({scrollTop:0}, 'fast');
				}
			});
		}
	});
	
	
	$('#contact_head #map ul').hover(function() {
		$('#contact_head #map ul li.mask').stop(true, true).fadeIn('fast');
	}, function() {
		$('#contact_head #map ul li.mask').stop(true, true).fadeOut('fast');
	});


	
	
	if( $('div.menu-header').length > 0 ) {
		set_active($('div.menu-header'),'load');
	}
	
	
	if( $('div.menu-header ul > li.menu-item').length > 0 ) {
		$('div.menu-header ul > li.menu-item').hover(function(e) {
//			set_active(this, 'hover');
		}, function(e) {
//			e.stopImmediatePropagation();
		});
	}
	
	
	

//	$.easing.def = "easeOutBack";
	
	if( $('#post_gallery').length > 0 ) {
		
		var counter = 0;
		
		$('.footer-gallery-item').each(function() { counter++; });
		
		var gallery_width = counter * 128;
		$('#gallery_items_inner_wrapper').css('width', gallery_width);
		
		
		var offset = gallery_width - $('#gallery_items').width();
		$('#left_advance').hover(function() {
			$('#gallery_items_inner_wrapper').animate({
				left: 0
			}, 1500);
		}, function() {
			$('#gallery_items_inner_wrapper').stop(true, false);
		});
		
		$('#right_advance').hover(function() {
			$('#gallery_items_inner_wrapper').animate({
				left: -offset
			}, 1500);
		}, function() {
			$('#gallery_items_inner_wrapper').stop(true, false);
		});
		
	}
	
//	$('#get_quote_dialog').load('/tss_contractAuthor/new_client');
	
	
	
	
	
	$('#get_quote_dialog').dialog({
		autoOpen: false,
		height: 550,
		width: 450,
		modal: true,
		closeOnEscape: true,
		position: ['center', 25]
	});
	
	
	$('#get_quote').click(function(e) {
		e.preventDefault();
		var load_height = 0, ready_height = 0, new_height;
		$('#get_quote_dialog').dialog('open');
		
		$('#get_quote_iframe').load( function() {
			GLOBALS.load_height = $('#get_quote_iframe').contents().find('#container').height() + 200;//90;
			set_height();
		});
		
		$('#get_quote_iframe').contents().find('#container').ready( function() {
			GLOBALS.ready_height = $('#get_quote_iframe').contents().find('#container').height() + 200;//90;
			set_height();
		});
		
		
		
			
//		}
		
//		new_height = (load_height > ready_height) ? load_height : ready_height;
		
		
	});


	if( $("a#single_image").length > 0 ) {
		$("a#single_image").fancybox();
	}



	
});



function set_active(that, type) {
	
	var $current;
	
	if(type == 'load') {
		$('div.menu-header ul li').each(function() {
			var class_name = $(this).attr('class');
		
			if( class_name.indexOf('current') > 0 ) {
				$current = $(this);
				while(1) {
					if( $current.parents('li.menu-item').length ) {
						$current = $current.parent();
					} else {
						break;
					}
				}
			} else if ( $current === undefined )  {
				$current = $('div.menu-header ul li.menu-item:first-child');
			}
		
		});
	} else if(type == 'hover') {
		$current = $(that);
	}
	
	
	
	$('#access div.menu-header ul#menu-primary > li').addClass('inactive');
	$('#access div.menu-header ul#menu-primary > li ul.sub-menu').hide();
	$current.removeClass('inactive').addClass('hovering');
	
	if( $current.find('ul.sub-menu').length > 0 ) {
		
		$current.find('ul.sub-menu').show(); 
		
		
		var sub_menu_loc = -( ($current.find('ul.sub-menu li:last-child').position().left + $current.find('ul.sub-menu li:last-child').width()) / 2) + ( $current.width() / 2 );
		
		var test_loc = -$current.position().left;
		
		if(sub_menu_loc < test_loc)
			sub_menu_loc = test_loc;
		
		$current.find('ul.sub-menu').css('left',sub_menu_loc);
		
		var $last_child = $current.find('ul.sub-menu li:last-child');
		var list_width = Math.ceil($last_child.position().left + $last_child.width());
		$current.find('ul.sub-menu').css('width',list_width);

		
		
	}
	
	var arrow_loc = ($current.position().left) + ($current.width() / 2);
	$('div#location_arrow').show();
	$('div#location_arrow').css('left',arrow_loc);
	
}



function close_dialogue() {
	$('#get_quote_dialog').dialog('close');
}

function set_height() {
//	alert(new_height);
//	new_height += 100;

	if( (GLOBALS.load_height > 25) && GLOBALS.load_height > $('#get_quote_iframe').height() ) {
		$('#get_quote_iframe').height(GLOBALS.load_height);
	} else if ( (GLOBALS.ready_height > 25) && GLOBALS.ready_height > $('#get_quote_iframe').height() ) {
		$('#get_quote_iframe').height(GLOBALS.ready_height);
	}
	
}

function move_top() {
//	alert('hi');
	$('#get_quote_dialog').scrollTop(1);
//	$('html, body').animate({scrollTop:0}, 'fast');
//	$('#get_quote_dialog').dialog({position: 'top');
	
	
}
