/**
 * NISSAN Polody
 *
 * Author: Dawid Rogaczewski
 * Author-URI: http://da-ro.de
 *
 * @package     catering
 * @copyright   Copyright (c) 2011 engage-media
 *		
 */
$(document).ready(function()
{
	
	// Main Teaser
	if($('#np_slider').length > 0){
		$('#np_slider').cycle({
			fx: 'fade',
			speed: 1100,
			timeout: 8500,
			pager: '#pager',
			pagerAnchorBuilder: buildPagination,
			easing: 'easeInOutCubic',
			pause: true,
			next: '#next',
			prev: '#prev'
		});	
	}

	inputVal();
	
	// init navi
	$(" #np_nav li ").hoverIntent(function(){
		$(this).addClass('over').find('ul:first').show();
	},function()
	{
		$(this).removeClass('over').find('ul:first').fadeOut('fast');
	});
	
	// Rounded Corner Image
	if($('img.rnd').length > 0){
		roundedImageCorners();
	}
	
	// Home Boxes
	if($('#np_topBoxes').length > 0){
		$('#np_topBoxes').find('li').each(
			function(){
				var t = $(this);
				var href = t.find('a').attr('href');
			
				t.hover(
					function(){
						t.css('cursor', 'pointer');
					}
				);
				
				t.click(
					function(){
						window.location = href;
						return false;
					}
				);
			}
		);
	}
	
	// Address Box
	//When page loads...
    if( $(".vCard").length > 0 ) {
   		var cityId = $('#np_city :selected').val();
   		var loadUrl = "ajax/load.php";
   		var loader = '<img src="'+basePath+'loader.gif" alt="Loading" id="loader" />';
		var vCard = $(".vCard");
		
		vCard.html(loader).load(loadUrl, {get: "city", id: cityId}, function() {
			$('#loader').fadeOut('slow');
		}); 
		
		$('#np_city').change(function() {
			vCard.fadeOut();
			vCard.fadeIn().html(loader).load(loadUrl, {get: "city", id: $('#np_city :selected').val()}, function() {
				$('#loader').fadeOut('slow');
			});
		});	
			
   	}
   	
   	// load Tabs
	if($('.tabs').length > 0)
	{
		initTabs();
	}
	
	// Offers boxes
	if($('.magazine-style .post, .blog-style .post').length > 0)
	{
		$('.magazine-style .post, .blog-style .post').hover(
			function(){
				$(this).toggleClass('over');
			}
		);
		
		$('.magazine-style .post, .blog-style .post').click(
			function(){
				window.location = $(this).find('a').attr('href');
				return false;
			}
		);
	}
	
	// Gallery
	if($('#gallery').length > 0)
	{
		var target = $('#gallery-img');
		var targetImg = target.find('img');
		var thumbs = $('#thumbnails');
		
		targetImg.load(function() {
   			$(this).fadeIn('fast'); 
  		});
		
		$("#thumbnails a:first").addClass("selected"); //Activate first thumb
		
		//On Click Event
		$("#thumbnails a").click(function()
		{
			var thisImg = $(this).attr("href");
			
			$("#thumbnails a").removeClass("selected"); //Remove any "active" class
			$(this).addClass("selected"); //Add "active" class to selected link
			
			/*targetImg.fadeOut('fast', function(){
				targetImg.attr('src', thisImg).fadeIn();
			});*/
			
			targetImg.stop().fadeOut('fase', function(){
				targetImg.attr('src', thisImg).removeAttr('height');
			});
						
			return false;
		});
	}
	
	// Get Post value on test drive request
	if( $('.page-id-132').length )
	{
		var model_hash = getUrlVars()['model'];
		var dealer_hash = getUrlVars()['dealer'];
		
		if(dealer_hash)
			$("#dealer option[value='"+dealer_hash+"']").attr('selected',true);
			
		if(model_hash)
			$("#model option[value='"+model_hash+"']").attr('selected',true);
	}
	
});

/* init input
--------------------------------------------------------------------- */
function inputVal(){
	swapValue = [];
	$("input.txt").each(function(i){
		swapValue[i] = $(this).val();
		$(this).focus(function(){
			if($(this).val() == swapValue[i]) {
				$(this).val("");
			}
			$(this).addClass("focus");
		}).blur(function(){
			if ($.trim($(this).val()) == "") {
				$(this).val(swapValue[i]);
				$(this).removeClass("focus");
			}
		});
	});
}

/* Rounded Image Corners
--------------------------------------------------------------------- */
function roundedImageCorners(){
   	$('img.rnd').one('load',function () {
		var img = $(this);
		var img_width = img.width();
		var img_height = img.height();
 
		// build wrapper
		var wrapper = $('<div class="rnd_wrap"></div>');
		wrapper.width(img_width);
		wrapper.height(img_height);
 
		// move CSS properties from img to wrapper
		wrapper.css('float', img.css('float'));
		img.css('float', 'none')
 
		wrapper.css('margin-right', img.css('margin-right'));
		img.css('margin-right', '0')
 
		wrapper.css('margin-left', img.css('margin-left'));
		img.css('margin-left', '0')
 
		wrapper.css('margin-bottom', img.css('margin-bottom'));
		img.css('margin-bottom', '0')
 
		wrapper.css('margin-top', img.css('margin-top'));
		img.css('margin-top', '0')
 
		wrapper.css('display', 'block');
		img.css('display', 'block')
  
		// wrap image
		img.wrap(wrapper);
 
		// add rounded corners
		img.after('<div class="tl"></div>');
		img.after('<div class="tr"></div>');
		img.after('<div class="bl"></div>');
		img.after('<div class="br"></div>');
		
	}).each(function(){
		if(this.complete) $(this).trigger("load");
	});
}

/* Cycle: Pagination
--------------------------------------------------------------------- */
function buildPagination(i, elem){
	var num = i+1;
	//var title = $(elem).find('h2').text();
	var title = '';
	var html = '<a href="#" title="' + title + '">' + num + '</a>';
	return html;
}

/* init Tabs
--------------------------------------------------------------------- */
function initTabs()
{
	$(".tabs_content").hide(); //Hide all content
	$("ul.tabs_nav li:first").addClass("selected").show(); //Activate first tab
	$(".tabs_content:first").show(); //Show first tab conten
		
	//On Click Event
	$("ul.tabs_nav li a").click(function()
	{
		$("ul.tabs_nav li").removeClass("selected"); //Remove any "active" class
		$(this).parent().addClass("selected"); //Add "active" class to selected tab
		$(".tabs_content").hide(); //Hide all tab content

		var activeTab = $(this).attr("href");
		$(activeTab).fadeIn();
		return false;
	});
}

/* Read a page's GET URL variables and return them as an associative array
--------------------------------------------------------------------- */
function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}
