function model(cYear,cModel,cBGimage,cInstock,cYoutubeLinks) {
	this.cYear=cYear;
	this.cMake='Kia';
	this.cModel=cModel;
	this.cBGimage=cBGimage;
	this.cInstock=cInstock;
	this.cYoutubeLinks= new Array();
}

function filtername(modelNameUC) {
	var modelNameLC = modelNameUC.toLowerCase();
	var modelObject = modelNameLC.replace(" ","");
	var modelClassId = modelNameLC.replace(" ","-");
	return { modelObject : modelObject , modelClassId : modelClassId };
}

jQuery(function($) {

function vThumbnails(youtubeLink) {
	jQuery('#video-container ul#list li').remove();
	jQuery.each(youtubeLink, function(index, value) {	
	 	var vURL = value;
		var vID = vURL.match(/v=(.*)/);
		$.getJSON("http://gdata.youtube.com/feeds/api/videos?q="+ vID[1] +"&alt=json-in-script&callback=?", function(data){
			var vTitle = data.feed.entry[0].title.$t;
			var youtubeID = data.feed.entry[0].id.$t;
			var youtubeID = youtubeID.replace("http://gdata.youtube.com/feeds/api/videos/", "");
			jQuery('#video-container ul#list').append('<li><a href="http://www.youtube.com/watch?v='+ youtubeID +'"><div class="thumbnail"><span><img src="http://img.youtube.com/vi/'+ youtubeID +'/default.jpg"></span></div><div class="link">'+vTitle+'</div></a></li>').show('fast', function () { jQuery('#video-container ul#list').animate({ opacity: 1 }, 1500 );
				jQuery('#thumbnails').removeClass('loading');																																																																								   			});
		});
	});
}


function vBGImage(a,b) {
	jQuery(a).hide();
	jQuery(a).load(function () {
    	jQuery('.image-preload').removeClass('loading').append(this);
    	jQuery(this).fadeIn();
	}).attr('src', b);
}

function vLinks(y,mk,md) {
	jQuery('#pre-qualified a').attr({ href: "FinancePreQualForm?vehicle:buy:year="+y+"&vehicle:buy:make="+mk+"&vehicle:buy:model="+md});
}

function vStock(s,y,mk,md) {
	if(s=="No"){
		jQuery('#pre-qualified, #test-drive').hide();
		jQuery('#inventory a span').replaceWith("<span>More<br/>Details</span>");
		jQuery('#inventory a').attr({ href: y+md});
	} else {
		jQuery('#pre-qualified, #test-drive').show();
		jQuery('#inventory a span').replaceWith("<span>View<br/>Inventory</span>");
		jQuery('#inventory a').attr({ href: "VehicleSearchResults?pageName=VehicleDetails&search=new&make="+mk+"&model="+md+"&minYear="+y });
	}
}

function paneVideo(v) {
	$.getJSON("http://gdata.youtube.com/feeds/api/videos?q="+ v +"&alt=json-in-script&callback=?", function(data){
				vTitle = data.feed.entry[0].title.$t;
				vContent = data.feed.entry[0].content.$t;
				jQuery("#video-pane h3").replaceWith('<h3>'+vTitle+'</h3>');
				jQuery("#video-pane #info").replaceWith('<div id="info">'+vContent+'</div>');
			});
			jQuery("#video-pane #video").replaceWith('<div id="video"><object width="400" height="249"><param name="movie" value="http://www.youtube.com/v/'+ v +'?version=3&rel=0&showinfo=0&color1=0xffffff&color2=0xffffff&autoplay=1&feature=player_embedded"></param><param name="allowFullScreen" value="true"></param><param name="wmode" valu="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ v +'?version=3&rel=0&showinfo=0&color1=0xffffff&color2=0xffffff&autoplay=1&feature=player_embedded" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" allowfullscreen="true" width="400" height="249"></embed></object></div>');
}

function youtubeVideo(w,h,ytl){
	var youtubeV = '<div id="obj"><object width="'+w+'" height="'+h+'"><param name="movie" value="http://www.youtube.com/v/'+ytl+'?version=3&rel=0&showinfo=0&color1=0xffffff&color2=0xffffff&autoplay=1&feature=player_embedded"></param><param name="allowFullScreen" value="true"></param><param name="wmode" valu="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ytl+'?version=3&rel=0&showinfo=0&color1=0xffffff&color2=0xffffff&autoplay=1&feature=player_embedded" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="transparent" allowfullscreen="true" width="'+w+'" height="'+h+'"></embed></object></div>';
	return youtubeV;
}

function ie6(){
	var announce = '<div id="upgrade-browser"><p>Please upgrade to one of the browsers below.  Youtube no longer supports Internet Explorer 6, which is why you\'re not able to view the videos.</p><a id="ie" href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Internet Explorer 8</a><a id="firefox" href="http://www.mozilla.com/en-US/?from=sfx&uid=267821&t=449">Firefox</a><a id="opera" href="http://www.opera.com/browser/">Opera</a><a id="chrome" href="http://www.google.com/chrome">Chrome</a><a id="safari" href="http://www.apple.com/safari/">Safari</a></div>';
	return announce;
}

var userAgent = navigator.userAgent;
var b = userAgent.split(";",2);

if (jQuery(".carousel").length) {
	var n = jQuery("#new-models .model").length;
	var modelLIwidth = jQuery("#new-models .model").width();
	var containerWidth = jQuery("#model-list").width();
	var modelULwidth = n * modelLIwidth;
	var scrollSpeed = 50;
	jQuery("#new-models").css({ width: modelULwidth});
	var amount = modelLIwidth;
	var maxScroll = modelULwidth - ( Math.floor(( modelULwidth / containerWidth )) * containerWidth );
	maxScroll = '-' + maxScroll;
	var currentPos = 0;

	var img = new Image();
	jQuery('.image-preload img').remove();
	jQuery('#thumbnails #list li').remove();
	jQuery("#video-container ul#list, .image-preload img").animate({ opacity: 0 });
	jQuery('.image-preload, #thumbnails').addClass('loading');
	var firstLoad = filtername(jQuery('#model-list li .active').attr('title'));
	vStock(eval(firstLoad.modelObject+".cInstock"),eval(firstLoad.modelObject+".cYear"),eval(firstLoad.modelObject+".cMake"),eval(firstLoad.modelObject+".cModel"));
	vLinks(eval(firstLoad.modelObject+".cYear"),eval(firstLoad.modelObject+".cMake"),eval(firstLoad.modelObject+".cModel"));
	vBGImage(img,eval(firstLoad.modelObject+".cBGimage"));
	vThumbnails(eval(firstLoad.modelObject+".cYoutubeLinks"));

jQuery("#new-model-container .button a").click(function () {
	if (jQuery(this).is(".next") || jQuery(this).is(".no-next")) {
		currentPos = parseInt(jQuery('#new-models').css('left')) - amount;
		if ( currentPos >= maxScroll ){
			jQuery('#new-models').animate({ left: '-='+amount }, scrollSpeed );
		} else {
			currentPos = maxScroll;
		}
	} else {
		currentPos = parseInt(jQuery('#new-models').css('left')) + amount;
		if ( currentPos < 0 ){
			jQuery('#new-models').animate({ left: '+='+amount }, scrollSpeed );
		} else if ( currentPos == 0 ){
			currentPos = 0;
			jQuery('#new-models').animate({ left: '+='+amount }, scrollSpeed );
		}
	}
	
	if( currentPos >= 0 ){
		jQuery(".prev").removeClass('prev').addClass('no-prev');
		if( currentPos == 0 ){
			jQuery(".no-next").removeClass('no-next').addClass('next');
		}
	} else if( currentPos == maxScroll){
		jQuery(".next").removeClass('next').addClass('no-next');
		if( currentPos < 0 ){
			jQuery(".no-prev").removeClass('no-prev').addClass('prev');
		}
	} else {
		jQuery(".no-next").removeClass('no-next').addClass('next');
		jQuery(".no-prev").removeClass('no-prev').addClass('prev');
	}
	return false;
	
});

	
jQuery('#new-model-container .model a').bind('click', function() {
	var model = jQuery(this);
	loadThis = filtername(jQuery(model).attr('title'));
	if (!jQuery(this).is(".active")){
		vStock(eval(loadThis.modelObject+".cInstock"),eval(loadThis.modelObject+".cYear"),eval(loadThis.modelObject+".cMake"),eval(loadThis.modelObject+".cModel"));
		jQuery('#thumbnails ul#list').hide();
		jQuery('.image-preload, #thumbnails').addClass('loading');
		jQuery('#new-model-container .model a.active').removeClass('active'); 
	 	jQuery(model).addClass('active');
		if(jQuery('#video-pane').is('.open-pane')){
			jQuery("#video-pane").fadeOut(function(){
				jQuery("#video-container").stop().animate({ width: "268px"}, 500, function (){
					jQuery("#video-pane #video").replaceWith('<div id="video"></div>');
					jQuery('#video-pane').removeClass('open-pane');
				});
				vLinks(eval(loadThis.modelObject+".cYear"),eval(loadThis.modelObject+".cMake"),eval(loadThis.modelObject+".cModel"));
				vBGImage(img,eval(loadThis.modelObject+".cBGimage"));
				jQuery('#thumbnails ul').stop().animate({ opacity: 0 }, 500, function(){
					vThumbnails(eval(loadThis.modelObject+".cYoutubeLinks"));	
				});
			});	
		} else {
			jQuery("#video-container").animate({ width: "268px"}, 500 );
			vLinks(eval(loadThis.modelObject+".cYear"),eval(loadThis.modelObject+".cMake"),eval(loadThis.modelObject+".cModel"));
			vBGImage(img,eval(loadThis.modelObject+".cBGimage"));
			jQuery('#thumbnails ul').stop().animate({ opacity: 0 }, 500, function(){
				vThumbnails(eval(loadThis.modelObject+".cYoutubeLinks"));	
			});
		}
	}
	return false;
});

jQuery('.carousel #thumbnails #list li a').livequery(function(){
	var vURL = jQuery(this).attr('href');
	var vID = vURL.match(/v=(.*)/);
	jQuery(this).bind('click', function() {
		if(b[1].match(/6.0/)){
			jQuery.facebox(ie6());
		} else {
			var $li = jQuery(this).parent();
			var liID = $li.parent().children().index( $li );
			if (jQuery(this).parents('li').is(".active")) {
				jQuery('#thumbnails li.active').removeClass('active');
				jQuery("#video-pane").fadeOut(function(){
					jQuery("#video-container").stop().animate({ width: "268px"}, 500 );
					jQuery('#video-pane').removeClass('open-pane');
				});
			} else if (!jQuery(this).parents('li').is(".active") && jQuery('#video-pane').is('.open-pane')) {
				jQuery('#thumbnails li.active').removeClass('active');
				jQuery(this).parents('li').addClass('active');
				paneVideo(vID[1]);
		   } else {
				jQuery('#video-pane').addClass('open-pane');
				jQuery(this).parents('li').addClass('active');
				paneVideo(vID[1]);
				jQuery("#video-container").stop().animate({ 
					width: "700px"}, 500, function (){
						jQuery("#video-pane").fadeIn();
				});
			}
		}
	return false;
    });
});


}

jQuery("#uvo a").click(function () {
	if(b[1].match(/6.0/)){
		jQuery.facebox(ie6());
	} else {
	jQuery.facebox(youtubeVideo(550,365,'s6CyT-1I1WI'));
		jQuery("#facebox .close, .facebox_overlayBG").livequery(function(){
			jQuery(this).click(function () {
				jQuery('#facebox .body .content').empty();
				return false;
			});
		});
	}
	return false;
});

jQuery("#video-pane #close a").click(function () {
	jQuery('#thumbnails li.active').removeClass('active');
	jQuery("#video-pane").fadeOut(function(){
		jQuery("#video-container").stop().animate({ width: "268px"}, 500 );
		jQuery("#video-pane #video").replaceWith('<div id="video"></div>');
	});
	jQuery('#video-pane').removeClass('open-pane');
	return false;
});

jQuery(".details-button a").click(function () {
  if (jQuery(this).is('.show')) {
	  jQuery(this).text('Hide Details');
	  jQuery(this).removeClass('show');
	  jQuery(this).parent().siblings('.details').css({display:'inline'});
  } else {
	  jQuery(this).text('Show Details');
	  jQuery(this).addClass('show');
	  jQuery(this).parent().siblings('.details').css({display:'none'});
  }
  return false;
});

jQuery(".department-list a").click(function () {
	if (jQuery(this).parent().is('.name')) {
		pName = jQuery(this).text();
	} else {
		pName = jQuery(this).parent().siblings('.name').children().text();
	}
	var personInfo = '<iframe scrolling="no" frameborder="0" src="http://www.cars911.com/kia/employee.php?nm='+pName+'"></iframe>';
	jQuery.facebox(personInfo);
	return false;
});

//jQuery(".offer-links li a, .offers .left .image a").click(function () {
//	var special = jQuery(this).attr('rel');
//	var personInfo = '<div class="appointment"><iframe scrolling="no" frameborder="0" src="http://www.cars911.com/kia/service.php?special='+special+'"></iframe></div>';
//	jQuery.facebox(personInfo);
//	return false;
//});

$('#tabs-info ul li a').click(function(){ //When any link is clicked
	if(!jQuery(this).is(".active")){
	  $('#tabs-info ul .active').removeClass('active'); // Remove active class from all links
	  $(this).parent().addClass('active'); //Set clicked link class to active
	  var currentTab = $(this).attr('rel'); // Set variable currentTab to value of href attribute of clicked link
	  $('#tabs-info .tab').hide(); // Hide all divs
	  $("#tabs-info "+currentTab).show(); // Show div with id equal to variable currentTab 
	}
	return false;
});

jQuery('.thumbnail-container li a').bind('click', function() {
	var type = $(this).parents('.thumbnail-container').children('.header').children('span').html();
	jQuery('#pic-vid-gallery #full-size .inner').addClass('loading');
	if (type=='Photos') {
		var imagelink = $(this).attr('href');
		var img = new Image();
		jQuery(img).load(function () {
			jQuery(this).hide();
			jQuery('#pic-vid-gallery #full-size img, #pic-vid-gallery #full-size #obj').remove();
			jQuery('#pic-vid-gallery #full-size .inner').removeClass('loading').append(this);
			jQuery(this).fadeIn();
		}).attr('src', imagelink);
	} else {
		if(b[1].match(/6.0/)){
			jQuery.facebox(ie6());
		} else {
			var videolink = $(this).attr('rel');
			jQuery('#full-size .inner object').hide();
			jQuery('#pic-vid-gallery #full-size img, #pic-vid-gallery #full-size #obj').remove();
			jQuery('#full-size .inner').removeClass('loading').append(youtubeVideo(479,308,videolink));
			jQuery('#full-size object').fadeIn();
		}

	}
	return false;
});

if (jQuery(".popup-question form").length) {
    jQuery(".popup-question form").validate({
		rules: {
			frm_nm: "required",
			frm_eml: {
				required: true,
				email: true
			},
			frm_qa: "required",
			empolyee_nm: "required",
			empolyee_eml: "required",
			dlr: "required"
		},
		messages: {
			frm_nm: "Please enter your name",
			frm_eml: "Please enter a valid email",
			frm_qa: "Please enter your question",
			empolyee_nm: "",
			empolyee_eml: "",
			dlr: ""
		},
		submitHandler: function() {
			jQuery('.popup-question form').hide();
			jQuery('.popup-question').addClass('loading');
			ajaxStringData = $('.popup-question form').serialize();
			jQuery.ajax({
   			type: "POST",
   			url: "http://www.cars911.com/kia/email.php",
   			data: ajaxStringData,
   			success: function(){
				jQuery('.popup-question form').remove();
				jQuery('.popup-question').removeClass('loading').append('<div class="confirm-message">Thank you, I will contact you shortly</div>');
  				}
			});
		}
	});
}
  
if (jQuery(".service-form form").length) {
	jQuery(".service-form form").validate({
		rules: {
			'vehicle:buy:year': "required",
			'vehicle:buy:make': "required",
			'vehicle:buy:model': "required",
			'service:preferred_day': "required",
			'service:preferred_time': "required",
			'service:alternate_day': "required",
			'service:alternate_time': "required",
			'contact:name:first': "required",
			'contact:name:last': "required",
			'contact:email': {
				required: true,
				email: true
			},
			'contact:phone:phone:day': "required",
			'preferred_contact': "required",
			'contact:address:home:postalcode': "required"
		},
		messages: {
			'vehicle:buy:year': "",
			'vehicle:buy:make': "",
			'vehicle:buy:model': "",
			'service:preferred_day': "",
			'service:preferred_time': "",
			'service:alternate_day': "",
			'service:alternate_time': "",
			'contact:name:first': "",
			'contact:name:last': "",
			'contact:email': "",
			'contact:phone:phone:day': "",
			'preferred_contact': "",
			'contact:address:home:postalcode': ""
		},
		submitHandler: function() {
			jQuery('.service-form form').hide();
			jQuery('.service-form').addClass('loading');
			ajaxStringData = $('.service-form form').serialize();
			jQuery.ajax({
   			type: "POST",
   			url: "http://www.cars911.com/kia/process.php",
   			data: ajaxStringData,
   			success: function(){
				jQuery('.service-form form').remove();
				jQuery('.service-form').removeClass('loading').append('<div id="confirm-message" class="confirm-message">Thank you, We will send you a confirmation shortly</div>');
  				}
			});
		}
	});
}

if (jQuery(".newsletter-form form").length) {
   	jQuery(".newsletter-form form").validate({
		rules: {
			'frm_nm': "required",
			'frm_eml': {
				required: true,
				email: true
			}
		},
		messages: {
			'frm_nm': "",
			'frm_eml': ""
		},
		submitHandler: function() {
			jQuery('.newsletter-form form').hide();
			jQuery('.newsletter-form').addClass('loading');
			ajaxStringData = $('.newsletter-form form').serialize();
			jQuery.ajax({
   			type: "POST",
   			url: "http://www.cars911.com/kia/nl-process.php",
   			data: ajaxStringData,
   			success: function(){
				jQuery('.newsletter-form form').remove();
				jQuery('.newsletter-form').removeClass('loading').append('<div id="confirm-message" class="confirm-message">Thank you, We will send you a confirmation shortly</div>');
  				}
			});
		}
	});
}

});

var baseurl = 'http://www.cars911.com/kia/images/';

var soul = new model("2010","Soul", baseurl+"soul-bg.jpg");

soul.cYoutubeLinks[0] = "http://www.youtube.com/watch?v=jRs2ujPvwjo";
soul.cYoutubeLinks[1] = "http://www.youtube.com/watch?v=mKIT3uyhCaA";
soul.cYoutubeLinks[2] = "http://www.youtube.com/watch?v=Ty-BMr6SVlI";

var forte = new model("2010","Forte", baseurl+"forte-bg.jpg");

forte.cYoutubeLinks[0] = "http://www.youtube.com/watch?v=OpPTxRxRla4";
forte.cYoutubeLinks[1] = "http://www.youtube.com/watch?v=p_vk9hfzFQM";
forte.cYoutubeLinks[2] = "http://www.youtube.com/watch?v=SIAzOlQNczI";
forte.cYoutubeLinks[3] = "http://www.youtube.com/watch?v=qu_HV0O0Azw";


var fortekoup = new model("2010","Forte Koup", baseurl+"forte-koup-bg.jpg");

fortekoup.cYoutubeLinks[0] = "http://www.youtube.com/watch?v=U8NdBE_H-Lw";
fortekoup.cYoutubeLinks[1] = "http://www.youtube.com/watch?v=raaw8CSxO6U";
fortekoup.cYoutubeLinks[2] = "http://www.youtube.com/watch?v=uoy3xs1QLmI";

var optima = new model("2011","Optima", baseurl+"optima-bg.jpg");

optima.cYoutubeLinks[0] = "http://www.youtube.com/watch?v=iD0zVP-DkOE";
optima.cYoutubeLinks[1] = "http://www.youtube.com/watch?v=08M2WZ7jrLI";
optima.cYoutubeLinks[2] = "http://www.youtube.com/watch?v=h72oYZf4AUw";

var sportage = new model("2011","Sportage", baseurl+"sportage-bg.jpg");

sportage.cYoutubeLinks[0] = "http://www.youtube.com/watch?v=Fc6JMVnHZpU";
sportage.cYoutubeLinks[1] = "http://www.youtube.com/watch?v=EHDRohYbEIQ";

var sorento = new model("2011","Sorento", baseurl+"sorento-bg.jpg");

sorento.cYoutubeLinks[0] = "http://www.youtube.com/watch?v=8Wcf25MFqTY";
sorento.cYoutubeLinks[1] = "http://www.youtube.com/watch?v=pIS7T5jarsE";
sorento.cYoutubeLinks[2] = "http://www.youtube.com/watch?v=1L9Ef5EPMys";
sorento.cYoutubeLinks[3] = "http://www.youtube.com/watch?v=j3rUfAXSkF0";
sorento.cYoutubeLinks[4] = "http://www.youtube.com/watch?v=Yy6ZGsT786s";
