/**
 * @author Dan
 */

var parts_hover_box = function()
{
	if ($('.tooltip').size() > 0) 
	{
		$('.tooltip').tooltip({
			bgcolor:'#1A1A1A',
			bordercolor:'#474747',
			cursor:'pointer',
			fontcolor : '#FFFFFF',
			fontsize : '12px',
			offsetX: 10,
			offsetY: 10,
			width : '320px'
		});				
	}
}

var input_focus = function()
{
	$('#wrapper input[type=text]').each(function()
	{
		var el = $(this);
		var v = el.val();

		if (v.length > 0) 
		{
			el.bind('focus', function(e)
			{
				if (el.val() == v) 
				{
					el.val('');
				}
			});	
			el.bind('blur', function(e)
			{
				if (el.val().length == 0) 
				{
					el.val(v);
				}
			});			
		}
	});
	
}
var banner_links = function()
{
	$('a[href=#cr], a[href=#cvr]').bind('click', function(e)
	{
		var span;
		var v;
		e.preventDefault();
		if($(this).attr('href') == '#cr')
		{
			span = $('li.nav4 span.services');
		}
		if($(this).attr('href') == '#cvr')
		{
			span = $('li.nav5 span.services');
		}		
		v = (span.css('visibility'));
		v = (v == 'visible') ? 'hidden' : 'visible';
		
		span.css('visibility', v);		
	});
}

var breadcrumb_fix = function()
{
	if (window.location.href.indexOf('prod') != -1) 
	{
		var active_nav = $('#nav a.active');
		var br2 = $('.breadcrumb-2 a');
		if (br2.size() > 0) 
		{
			var brtxt = br2.html().replace('Car', 'Vehicle');
			brtxt = brtxt.replace('Commercial Vehicle', 'Commercial');
			if (brtxt != active_nav.html()) 
			{
				active_nav.removeClass('active');
				$('#nav a:contains("' + brtxt + '")').addClass('active');
			};
		};
	};	
}

var car_image_loader = function()
{
	if ($('#home_car_image').size() > 0) 
	{
		var el = $('#home_car_image');
		var src = el.attr('src');
		el.css('visibility', 'hidden');
	
		el.bind('load', function()
		{
			$(this).css('visibility', 'visible');
		});
		
	};
}

var app = function()
{
	breadcrumb_fix();
	banner_links();
	input_focus();
	parts_hover_box();
	car_image_loader();
	
	$(window).resize(parts_hover_box());
	
	// LOOPED SLIDER
	if ($('#loopedSlider:not(.gun) div.slides > div').size() > 0) 
	{
		$(function(){
			$('#loopedSlider').loopedSlider({
				autoStart: 12000,
				restart: 12000,
				dynamicLoad: true
				
			});
		});	}
		
		
		if ($('#loopedSlider.gun div.slides > div').size() > 0) 
	{
		$(function(){
			$('#loopedSlider.gun').loopedSlider({
				autoStart: 12000,
				restart: 12000
			});
		});	}


	//Caption Sliding (Partially Hidden to Visible)
	$('.boxgrid.caption').hover(function(){
		$(".cover", this).stop().animate({top:'0px'},{queue:false,duration:520});
	}, function() {
		$(".cover", this).stop().animate({top:'168px'},{queue:false,duration:520});
	});
	
	// CONTACT
	$('#contactSlide').css('top', function(index)
	{
		var t = -1 * ($(this).height() + 15);
		return t;
	});
	
	$("li.nav10 a").live('click', 
		function()
		{
			$('#contactSlide').animate({top:'0px'}, {queue:false,duration:600});
			$('#contactSlide > .handle').addClass('out');
			
			return false;
		}
	);	
	
	var lang = 'en-gb';
	
	var lang = (get_lang().length == 0) ? lang : get_lang();
	
	var els = $('#app_contact_form-contactcountry, #app_topgunsignup_form-tsucountry, #carotm_form-cotmcountry');
	
	// France = 250
	// Germany = 276
	// Italy = 380
	// Spain = 724
	if (lang != 'en-gb') 
	{
		if (lang == 'fr-fr') {els.val(250);};
		if (lang == 'de-de') {els.val(276);};
		if (lang == 'it-it') {els.val(380);};
		if (lang == 'es-es') {els.val(724);};						
	};
	
	$('#form-app_contact_form').live('submit', function(e)
	{
		e.preventDefault();
		
		$.ajax({
			type: 'POST',
			url: get_url() + 'blocks_contact/view',
			data :$(this).serialize(),
			success: function(resp)
			{
				$('#contactSlide').replaceWith(resp);
			}
		});
	});

	$('.close-contact').live('click', function()
	{
		var t = -1 * ($('#contactSlide').height() + 15 );

		$('#contactSlide > .handle').removeClass('out');			
		$('#contactSlide').animate({top: t + 'px'}, {queue:false,duration:600});
		
	})

	// FANCYBOX POPUPS
	if ($(".popUp").size() > 0) 
	{
		$(".popUp").fancybox({
			titlePosition			: 'inside',
			'transitionIn'			: 'elastic',
			'transitionOut'			: 'elastic',
			'opacity'				: true,
			'hideOnContentClick'	: false
		});
	};
	
	// VIDEOS
	if ($(".fb-vid").size() > 0) 
	{
		if ($(".fb-vid.current-vid").size() > 0) 
		{
			vidfb($('.fb-vid.current-vid'));
		}
		$(".fb-vid").bind('click', function()
		{
			vidfb($(this));
			return false;
		});
	};
	
	// Exploded Views
	if ($(".ex-vid").size() > 0) 
	{
		$(".ex-vid").bind('click', function()
		{
			expl_vid($(this));
			return false;
		});
	};	
	
	// Rotating View
	if ($(".fb-rv").size() > 0) 
	{
		if ($(".fb-rv.current-vid").size() > 0) 
		{
			rvfb($('.fb-rv.current-vid'));
		}
		$(".fb-rv").bind('click', function()
		{
			rvfb($(this));
			return false;
		});
	};
	
	// PRODUCT TABS
	if ($("#product").size() > 0) 
	{
		$(function() {
			$('#product').tabs({ fxFade: true, fxSpeed: 'fast' });
		});
	}
	
	if ($('.infiniteCarousel').size() > 0) 
	{
		$('.infiniteCarousel').infiniteCarousel();
	}



 }

// Rotating view fancybox
var rvfb = function(el)
{
	$.fancybox({
		type					: 'swf', 
		padding					: 0,
		width					: 800,
		height					: 600,		
		href					: el.attr('href').replace(new RegExp("watch\\?v=", "i"), 'v/'),
		swf						: {wmode: 'transparent', 'allowfullscreen' : 'true'},
		titlePosition			: 'inside',
		'transitionIn'			: 'elastic',
		'transitionOut'			: 'elast1ic',
		//'opacity'				: true,
		onStart: function()
		{
			$('#fancybox-content').css('background-color', '#000');
		}
		// 'hideOnContentClick'	: false
	});
}

var expl_vid = function(el)
{
	$.fancybox({
		type					: 'iframe', 
		href					: el.attr('href'),		
		padding					: 0,
		width 					: 900,
		height					: 600, 
		titlePosition			: 'inside'
	});

}


// Video fancybox
var vidfb = function(el)
{
	$.fancybox({
		type					: 'swf', 
		padding					: 0,
		href					: el.attr('href').replace(new RegExp("watch\\?v=", "i"), 'v/'),
		swf						: {wmode: 'transparent', 'allowfullscreen' : 'true'},
		titlePosition			: 'inside'
		// 'transitionIn'			: 'elastic',
		// 'transitionOut'			: 'elastic',
		//'opacity'				: true,
		
		// 'hideOnContentClick'	: false
	});
}




  // simple example, using all default options
	if (  $('div.expandable p').size() > 0) 
	{
		$('div.expandable p').expander();
	};
  // *** OR ***
  
  // override some default options
  //$('div.expandable p').expander({
    //slicePoint:       80,  // default is 100
    //expandText:         '[...]', // default is 'read more...'
    //collapseTimer:    5000, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    //userCollapseText: '[^]'  // default is '[collapse expanded text]'
  //});
  




 $(document).ready(app);

