var hoverIntentConfig = {    
     over:  function() {
				jQuery(this).css({'z-index' : '10'}); //Add a higher z-index value so this image stays on top
				jQuery(this).find('img').show('slow');		
			}, // function = onMouseOver callback (REQUIRED)    
     timeout: 500, // number = milliseconds delay before onMouseOut    
     out:   function() {
				jQuery(this).css({'z-index' : '0'});
				jQuery(this).find('img').hide('slow');		
			} // function = onMouseOut callback (REQUIRED)    
};
 
jQuery(document).ready(function() {
	
	var contactFormNameDefault = jQuery("input#Form_ContactForm_Name").val();
	var contactFormEmailDefault = jQuery("input#Form_ContactForm_Email").val();
	var contactFormMessageDefault = jQuery("input#Form_ContactForm_Message").val();
	
	jQuery("ul#nav li").hover(function() {
			jQuery(this).addClass("over")			
		}, function() {
			jQuery(this).removeClass("over");		
	});
	
	//Contact Form
	jQuery("input#Form_ContactForm_Name").focus(function(){		
		if(jQuery(this).val()==contactFormNameDefault){
		   jQuery(this).val("");
		}
    });
	jQuery("input#Form_ContactForm_Name").blur(function(){		
		if(jQuery(this).val()==""){
		   jQuery(this).val(contactFormNameDefault);
		}
    });
	
	jQuery("input#Form_ContactForm_Email").focus(function(){
            if(jQuery(this).val()==contactFormEmailDefault){
               jQuery(this).val("");
            }
    });
	jQuery("input#Form_ContactForm_Email").blur(function(){
            if(jQuery(this).val()==""){
               jQuery(this).val(contactFormEmailDefault);
            }
    });
	
	jQuery("textarea#Form_ContactForm_Message").focus(function(){
		if(jQuery(this).val()==contactFormMessageDefault){
		   jQuery(this).val("");
		}
    });
	jQuery("textarea#Form_ContactForm_Message").blur(function(){
		if(jQuery(this).val()==""){
		   jQuery(this).val(contactFormMessageDefault);
		}
    });
	
	//Product contact form
	jQuery("input#Form_ProductForm_Name").attr({ value: 'Enter your name' }).focus(function(){
            if(jQuery(this).val()=="Enter your name"){
               jQuery(this).val("");
            }
    });
	jQuery("input#Form_ProductForm_Email").attr({ value: 'Enter your email' }).focus(function(){
            if(jQuery(this).val()=="Enter your email"){
               jQuery(this).val("");
            }
    });
	jQuery("textarea#Form_ProductForm_Message").val('Your message here...').focus(function(){
            if(jQuery(this).val()=="Your message here..."){
               jQuery(this).val("");
            }
    });
	
	//Dealership contact form
	jQuery("input#Form_DealershipForm_Name").attr({ value: 'Enter your name' }).focus(function(){
            if(jQuery(this).val()=="Enter your name"){
               jQuery(this).val("");
            }
    });
	jQuery("input#Form_DealershipForm_Email").attr({ value: 'Enter your email' }).focus(function(){
            if(jQuery(this).val()=="Enter your email"){
               jQuery(this).val("");
            }
    });
	jQuery("textarea#Form_DealershipForm_Message").val('Your message here...').focus(function(){
            if(jQuery(this).val()=="Your message here..."){
               jQuery(this).val("");
            }
    });

	jQuery('a#product-interested-btn').click(function() {
		
		if(jQuery('#ProductForm').css('display') == 'block'){					//Slide Up
			jQuery('#ProductForm').slideUp('slow', function(){
				jQuery("#rightcol").height(jQuery("#rightcol").height() - 270);
			});
		}
		else {
			jQuery("#rightcol").height(jQuery("#rightcol").height() + 270);		//Slide Down
			jQuery('#ProductForm').slideDown('slow');
		}		
		return false;

	});
	
	//Categories hover effect
	jQuery("ul#categories").children("li").hover(function() {
		jQuery(this).toggleClass('over'); 
	}, function() {
		jQuery(this).toggleClass('over'); 
	});
	//Latest News hover effect
	jQuery("ul#LatestNews").children("li").hover(function() {
		jQuery(this).toggleClass('over'); 
	}, function() {
		jQuery(this).toggleClass('over'); 
	});
	
	//Product thumbnails
	
	jQuery("ul#products li").hoverIntent( hoverIntentConfig );
	//jQuery("#products li").hoverIntent( function() {$(this).addClass("hovering"); alert('hi');}, function() {alert('ho');} );
	//jQuery("#products li").hover( function() {alert('hi');}, function() {alert('ho');} );
	
	/*jQuery("ul#products li").hover(function() {
		jQuery(this).css({'z-index' : '10'}); //Add a higher z-index value so this image stays on top
		jQuery(this).find('img').toggle('slow');
		} , function() {
		jQuery(this).find('img').toggle('slow');
		jQuery(this).css({'z-index' : '0'}); //Set z-index back to 0 
	});*/
	
	//Countries dropdown
	var base = jQuery('base').attr('href');
	jQuery('#countries').change(function() {
		getRegions(jQuery(this).val());
	  //jQuery('#ajaxContent').load(jQuery('base').attr('href') + 'dealers/Regions/' + jQuery(this).val());
	  
	  //loadAjax('ajaxContent', '{$URLSegment}/dealers/Regions/' + jQuery(this).val());
	  return false;
	});
	/* FOR THE SWEDISH SITE*/
	jQuery('#countries-sv').change(function() {
		getRegionsSV(jQuery(this).val());
	  return false;
	});

	
	
	
	//Dealership Information Button
	jQuery('#dealership-info-btn').click(function() {
		jQuery('#DealershipForm').slideToggle('slow');
		return false;

	});	
	
	//Contact Business Card

	// get original img width 
	var oWidth = jQuery('img.bcard').width();

	// get img height 
	var oHeight = jQuery('img.bcard').height();

	//You could always multiply the multiplier if you want to make the image adjust to a larger size.  Keep in mind though it will get more blurry
	var mpx = (oWidth / oHeight);


	jQuery('img.bcard').hover(function(){
		  jQuery(this)
			
			//stops the event from happening in case of an abrupt mouseOut
			.stop()

			//custom animation effect to change the width and height of the img
			.animate({

				//take the original width/height X multipler and tag on the 'px'
				width: '700px',
				height: '403px'

				//space the animation out over 1 sec (deals in milliseconds)
			},1000);
		},
		//this is just like a mouseOut effect to take the img back to the original size
		function(){
				jQuery(this)

					 //stops the event from happening in case of an abrupt mouseOut
					.stop()

					//this animation shrinks the image back to original size
					.animate({
						 width: oWidth +'px',
						 height: oHeight +'px'
					},1000);

		});
		
	//Initialise the banner slideshow	
	jQuery("#slides").sfSlideShow(
        {
            controlPanel: 0,
            speed: 5000,
			fadeDuration: 'slow'
        }
    ); 
	

	
});

function autoFill(id, v){
	$(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "#333" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#b2adad" }).val(v);
		}
	});

}

function setTopImage(headerImage) {
	var base = jQuery('base').attr('href');
	jQuery("div#top").css("background-image", "url('"+base+"themes/pointguard/images/top-"+headerImage+".jpg')");
}

function loadAjax(loadIntoElID, page) {
	var completeURL = jQuery('base').attr('href') + page; //NOTE THAT jQuery('base').attr('href') should work as the base tag should be included in your header.
	var imgHtml = '<img src="mysite/javascript/loading.gif" alt="loading . . ." />';
	jQuery("#" + loadIntoElID).html(imgHtml);
	jQuery("#" + loadIntoElID).load(URL, {}, function() {/* another function here that runs after content has been loaded */});
	return false;
}

function getRegions(countryId){
	jQuery.ajax({
		type: "POST",
		url: jQuery('base').attr('href') + "dealers",
		data: 	"action=get_regions&countryid=" + countryId,
		success: function(html){
			jQuery("#regions").html(html);
			jQuery('#regions').removeAttr('disabled');
			jQuery('#cities').attr('disabled', 'disabled');
			jQuery('#DealerList').hide();

			jQuery('#regions').change(function() {
				getCities(jQuery(this).val());
				//jQuery('#ajaxContent').load(jQuery('base').attr('href') + 'dealers/Cities/' + jQuery(this).val());
				return false;
			});
		}
	});
	
}
/* FOR THE SWEDISH SITE*/
function getRegionsSV(countryId, Country){
	jQuery.ajax({
		type: "POST",
		url: jQuery('base').attr('href') + "aterforsaljare",
		data: 	"action=get_regions&countryid=" + countryId,
		success: function(html){
			html = html.replace('Norway', 'Norge');
			html = html.replace('Select a region', 'Välj en region');
			jQuery("#regions").html(html);
			jQuery('#regions').removeAttr('disabled');
			jQuery('#cities').attr('disabled', 'disabled');
			jQuery('#DealerList').hide();

			jQuery('#regions').change(function() {
				getCitiesSV(jQuery(this).val());
				//jQuery('#ajaxContent').load(jQuery('base').attr('href') + 'dealers/Cities/' + jQuery(this).val());
				return false;
			});
		}
	});
	
}

function getCities(regionId){
	jQuery.ajax({
		type: "POST",
		url: jQuery('base').attr('href') + "dealers",
		data: 	"action=get_cities&regionid=" + regionId,
		success: function(html){
			jQuery("#cities").html(html);
			jQuery('#cities').removeAttr('disabled');
			jQuery('#DealerList').hide();
			jQuery('#cities').change(function() {
				getDealers(jQuery(this).val());
			  //jQuery('#ajaxContent').load(jQuery('base').attr('href') + 'dealers/Cities/' + jQuery(this).val());
			  return false;
			});
		}
	});
}
/* FOR THE SWEDISH SITE*/
function getCitiesSV(regionId){
	jQuery.ajax({
		type: "POST",
		url: jQuery('base').attr('href') + "aterforsaljare",
		data: 	"action=get_cities&regionid=" + regionId,
		success: function(html){
			html = html.replace('Select a city', 'Välj en stad');
			jQuery("#cities").html(html);
			jQuery('#cities').removeAttr('disabled');
			jQuery('#DealerList').hide();
			jQuery('#cities').change(function() {
				getDealersSV(jQuery(this).val());
			  //jQuery('#ajaxContent').load(jQuery('base').attr('href') + 'dealers/Cities/' + jQuery(this).val());
			  return false;
			});
		}
	});
}
function getDealers(cityId){
	jQuery.ajax({
		type: "POST",
		url: jQuery('base').attr('href') + "dealers",
		data: 	"action=get_dealers&cityid=" + cityId,
		success: function(html){
			jQuery("#dealers").html(html);
			jQuery('#DealerList').show();
			
			jQuery('ul#dealers li').click(function() {
				getDealer(jQuery(this).attr('id'));
			});
		}
	});
	
}
function getDealersSV(cityId){
	jQuery.ajax({
		type: "POST",
		url: jQuery('base').attr('href') + "aterforsaljare",
		data: 	"action=get_dealers&cityid=" + cityId,
		success: function(html){
			
			jQuery("#dealers").html(html);
			jQuery('#DealerList').show();
			
			jQuery('ul#dealers li').click(function() {
				getDealerSV(jQuery(this).attr('id'));
			});
		}
	});
	
}
function getDealer(dealerId){
	jQuery.ajax({
		type: "POST",
		dataType: 'json',
		url: jQuery('base').attr('href') + "dealers",
		data: 	"action=get_dealer&dealerid=" + dealerId,
		success: function(json){
			//console.log(json);
			jQuery('#DealerInfoClose').click(function() {
				jQuery('#DealerInfo').hide();
				//jQuery('#DealersMap').show();				
			});
					
			jQuery('#DealersMap').hide();
			jQuery("#DealerInfoContent").html(json['Content']);	
			jQuery("#DealerInfoLogo").find('img').attr('src', jQuery('base').attr('href') + json['LogoFilename']);	
			jQuery('#DealerInfo').slideDown('slow');				
			
		},
		error: function() {
			//console.log('Error!');
		}
	});
	
}
function getDealerSV(dealerId){
	jQuery.ajax({
		type: "POST",
		dataType: 'json',
		url: jQuery('base').attr('href') + "aterforsaljare",
		data: 	"action=get_dealer&dealerid=" + dealerId,
		success: function(json){
			//console.log(json);
			jQuery('#DealerInfoClose').click(function() {
				jQuery('#DealerInfo').hide();
				//jQuery('#DealersMap').show();				
			});
						
			jQuery('#DealersMap').hide();
			jQuery("#DealerInfoContent").html(json['Content']);	
			jQuery("#DealerInfoLogo").find('img').attr('src', jQuery('base').attr('href') + json['LogoFilename']);	
			jQuery('#DealerInfo').slideDown('slow');				
			
		},
		error: function() {
			//console.log('Error!');
		}
	});
	
}


	
//function makeTall(){  jQuery(this).animate({"height":75},200);}
//function makeShort(){ jQuery(this).animate({"height":50},200);}

function makeTall(){  
	alert("whoop:" + this);
	jQuery(this).css({'z-index' : '10'}); //Add a higher z-index value so this image stays on top
	jQuery(this).find('img').toggle('slow');
}
function makeShort(){ jQuery(this).animate({"height":50},200);}


