var blank = new Image();
blank.src = 'images/blank.gif';

jQuery(document).ready( function(){

	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		jQuery('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
	
	if (jQuery('#LeftCallToAction *').length <= 0) jQuery('#LeftCallToAction').hide();
	if (jQuery('#RightCallToAction *').length <= 0) jQuery('#RightCallToAction').hide();

	jQuery('.button').hover( function(){
		jQuery(this).attr('src',jQuery(this).attr('src').replace("_inactive","_active"));
	}, function() {
		jQuery(this).attr('src',jQuery(this).attr('src').replace("_active","_inactive"));
	});

	jQuery('.submitButton').hover( function(){             
		jQuery(this).addClass('submit_active');
	}, function() {
		jQuery(this).removeClass('submit_active');
	});
	
	jQuery('#FormContents_0_0_btnValidate').hover( function(){             
		jQuery(this).addClass('check_availability_active');
	}, function() {
		jQuery(this).removeClass('check_availability_active');
	});
	
	jQuery('.SignUpSubmitButton > input').hover( function(){             
		jQuery(this).addClass('create_website_active');
	}, function() {
		jQuery(this).removeClass('create_website_active');
	});

	jQuery('.plan_enquiry, #OpenForm').click( function() {
	    var enquiryFormTitle = 'Ask about a Xevo Website';
	    if (jQuery(this).attr('formtitle') != null && jQuery(this).attr('formtitle') != undefined ) enquiryFormTitle = jQuery(this).attr('formtitle') ;
	    jQuery('#FormContent .form > form h2:first').html(enquiryFormTitle);
		
		if (jQuery('body').scrollTop()+jQuery('html').scrollTop() > 0 ) {
			jQuery('html,body').animate({scrollTop : 0},400, function() {
				jQuery("#FormContent").slideDown( function(){					
					jQuery('#OpenForm').fadeOut('fast');
				});
			});
		} else {
			jQuery("#FormContent").slideDown( function(){					
				jQuery('#OpenForm').fadeOut('fast');
			});
		}
	});

	jQuery('#CloseForm').click( function() {  
		jQuery("#FormContent").slideUp( 350, function(){
			jQuery('#OpenForm').fadeIn('fast');
		});
	});  

	// main toolbar nav
	jQuery('.level2').hide();
	jQuery('.selectedPage > .level2').show();		
	jQuery('.mainMenu > li').hover( function(){ jQuery(this).setMenuState()});	
	if (jQuery('.selectedPage').parents('li').length > 0) jQuery('.selectedPage').parents('li').setMenuState();
    
});

$.fn.setMenuState = function() {   
	if (jQuery(this).children('ul').length > 0 ){
		jQuery('.mainMenu li').removeClass('active');
		jQuery(this).addClass('active');
		jQuery('.level2').hide();
		jQuery(this).children('.level2').show();		
	}
};

jQuery.fn.cancelEvent = function(event) { 
	if(event){ 
		event.returnValue=false;
		event.cancelBubble=true; 
		try { event.preventDefault(); } catch(ex){  }
		try { event.stopPropagation(); } catch(ex){ }
	} 
	return false;
}

function fixPng(png) {
	var src = png.src;
	if (!png.style.width) { png.style.width = jQuery(png).width(); }
	if (!png.style.height) { png.style.height = jQuery(png).height(); }
	png.onload = function() { };
	png.src = blank.src;
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

