if ( typeof( window[ 'siteRoot' ] ) == "undefined" ) siteRoot='';
var galleryImages = new Array('RUN3R','XOV3R','RNG3R','CRZ3R','GLF3R');
var firstGalImage = Math.floor( Math.random() * galleryImages.length );
var navLGalImage = firstGalImage - 1;
if (navLGalImage < 0) navLGalImage = galleryImages.length - 1;
var navRGalImage = firstGalImage + 1;
if (navRGalImage > ( galleryImages.length - 1 )) navRGalImage = 0;

// remap jQuery to $
(function($){
	
	function preload(arrayOfImages) {
		$(arrayOfImages).each(function(){
			$('<img/>')[0].src = this;
			// Alternatively you could use:
			// (new Image()).src = this;
		});
	}
	
	preload([
		siteRoot+'img/gallery/lyric-RUN3R-1.jpg',
		siteRoot+'img/gallery/lyric-XOV3R-1.jpg',
		siteRoot+'img/gallery/lyric-RNG3R-1.jpg',
		siteRoot+'img/gallery/lyric-CRZ3R-1.jpg',
		siteRoot+'img/gallery/lyric-GLF3R-1.jpg'
	]);
	
	function openBGI() {
		var match = RegExp('[?&]bgi=([^&]*)').exec(window.location.search);
		if (match) {
			var bgi = match && decodeURIComponent(match[1].replace(/\+/g, ' '));
			$('#supersized a.activeslide img').attr('src','img/backgrounds/'+bgi);
			$('#viewBackground').click();
		}
	}

	function slide(dir,gID,nID) {
		if (dir == 'navR') {
			var galWrapStartL = '0px';
			var galWrapEndL = '-757px';
			var imageDivEq = 0;
		}
		else {
			var galWrapStartL = '-757px';
			var galWrapEndL = '0px';
			var imageDivEq = 1;
		}
		var LID = parseInt(nID) - 1;
		if (LID < 0) LID = galleryImages.length - 1;
		var RID = parseInt(nID) + 1;
		if (RID > ( galleryImages.length - 1 )) RID = 0;
		$('#gallery .navR').attr('id','i'+RID);
		$('#gallery .navL').attr('id','i'+LID);
		$('#gallery .image').wrap('<div class="galWrap"></div>');
		$('#gallery .galWrap').css('left',galWrapStartL);
		if (dir == 'navR')
			//$('#gallery .galWrap').append('<div class="image"><img src="+siteRoot+'img/gallery/lyric-'+galleryImages[nID]+'-1.jpg" id="i'+nID+'" /><a class="gold details" href="?section=Models&page='+galleryImages[nID]+'">'+galleryImages[nID]+' Details</a><a class="gold studies" href="?page=Case-Studies&page='+galleryImages[nID]+'">Case Studies</a><a class="gold video" href="#vid-'+galleryImages[nID]+'">Watch Video</a></div>');
			$('#gallery .galWrap').append('<div class="image"><'+siteRoot+'img src="img/gallery/lyric-'+galleryImages[nID]+'-1.jpg" id="i'+nID+'" /></div>');
		else
			//$('#gallery .galWrap').prepend('<div class="image"><'+siteRoot+'img src="img/gallery/lyric-'+galleryImages[nID]+'-1.jpg" id="i'+nID+'" /><a class="gold details" href="?section=Models&page='+galleryImages[nID]+'">'+galleryImages[nID]+' Details</a><a class="gold studies" href="?page=Case-Studies&page='+galleryImages[nID]+'">Case Studies</a><a class="gold video" href="#vid-'+galleryImages[nID]+'">Watch Video</a></div>');
			$('#gallery .galWrap').prepend('<div class="image"><'+siteRoot+'img src="img/gallery/lyric-'+galleryImages[nID]+'-1.jpg" id="i'+nID+'" /></div>');
		$('#gallery .galWrap').stop(true,true).animate({ left: galWrapEndL },600,function(){
			$('#gallery .image:eq('+imageDivEq+')').remove();
			$(this).children().unwrap();
		});
	}
	
	function addpowerzoomInit(){
		$('img.zoom').addpowerzoom({
			defaultpower: 2,
			powerrange: [2,4],
			largeimage: $('img.zoom').attr('src').replace('gallery/','gallery/larges/'),
			magnifiersize: [200,200]
		});
	}
	
	function shadowsInit() {
		$('.bgShadow').each(function(){
			$(this).css({
				width: $(this).next('.bgWhite').width(),
				height: $(this).next('.bgWhite').height(),
				top: $(this).next('.bgWhite').css('top'),
				left: $(this).next('.bgWhite').css('left')
			});
		});
	}
	
	function formatTitle(title, currentArray, currentIndex, currentOpts) {
		if (title) return '<a class="gold" href="mailto:?subject=Check out this pic of a ' + title.substring(0,title.length-2) + '&amp;body=http://lyricmotion.com/?section=Media%26page=Photo%26photo=' + title + '">email this image</a>';
	}


	//same as $(document).ready();
	$(function() {
	
		$('nav li').each(function() {
			if ($(this).find("ul").length > 0) {
				$(this).mouseenter(function() {
					$(this).find("ul").stop(true, true).show(0);
				});
				$(this).mouseleave(function() {
					$(this).find("ul").stop(true, true).hide(0);
				});
			}
		});
		$('nav a.On').hover(function(){
			$(this).removeClass('On');
		},function(){
			$(this).addClass('On');
		});
		$('nav ul ul').hover(function(){
			$(this).closest('li').children('a').addClass('stillOn');
			$(this).closest('li').children('a.On').removeClass('On').addClass('tempOff');
		},function(){
			$(this).closest('li').children('a').removeClass('stillOn');
			$(this).closest('li').children('a.tempOff').removeClass('tempOff').addClass('On');
		});
	
		$('#gallery .image img').attr({ src: siteRoot+'img/gallery/lyric-'+galleryImages[firstGalImage]+'-1.jpg', id: 'i'+firstGalImage });
	// walti	$('#gallery .image a.details').attr('href','?section=Models&page='+galleryImages[firstGalImage]).text(galleryImages[firstGalImage]+' Details');
	//	$('#gallery .image a.studies').attr('href','?page=Case-Studies&page='+galleryImages[firstGalImage]);
	//	$('#gallery .image a.video').attr('href','#vid-'+galleryImages[firstGalImage]);
		
		$('#gallery .navL').attr('id','i'+navLGalImage);
		$('#gallery .navR').attr('id','i'+navRGalImage);
		
		$('.fancybox').fancybox({
			overlayOpacity: 0.6,
			overlayColor: '#000000',
			cyclic: true,
			padding: 0,
			titleFormat: formatTitle
		});
		
		$('#prevslide').click(function(){
			imgLink = "http://lyricintl.com/"+$('#supersized a:eq(0) img').attr('src');
			$('#bgiEmail').attr('href',"mailto:?body="+imgLink);
		});
		
		$('a#viewBackground').click(function(){
			if ($(this).hasClass('Open')) {
				$(this).removeClass('Open').text("View Background Images");
				if ($('#pauseplay').attr('src') == siteRoot+"img/supersized/pause_dull.png") {
					$('#pauseplay').click();
				}
				$('#wrapperBG').fadeOut(300,function(){
					$('#wrapper').fadeIn(300);
				});
			}
			else {
				$(this).addClass('Open').text("View Full Website");
				$('#wrapper').fadeOut(300,function(){
					$('#wrapperBG').fadeIn(300);
					$('body').height('auto');
				});
			}
		});
		
		$('a.bgiDropLink').click(function(){
			var bgiDLID = $(this).attr('id').substring(1);
			if ($(this).hasClass('Open')) {
				$(this).removeClass('Open');
				$('div.bgiDropBox#'+bgiDLID).slideUp(200);
			}
			else {
				$(this).addClass('Open');
				$('div.bgiDropBox#'+bgiDLID).slideDown(300);
			}
		});
		
		$('#gallery .navR,#gallery .navL').click(function(){
			if (!$('#gallery .galWrap').is(":animated")) {
				var gID = $('#gallery .image img').attr('id').substring(1);
				var nID = $(this).attr('id').substring(1);
				var dir = $(this).attr('class')
				slide(dir,gID,nID);
			}
		});
		
		$('#modelGallery .navThumbs img').live('click',function(){
			var curID = $('#modelGallery > img').attr('id');
			var newID = $(this).attr('id')
			$(this).attr({ src: siteRoot+'img/models/gallery/thumbs/LYRIC-'+curID+'.jpg', id: curID });
			$('#modelGallery > img').remove();
			$('<img src="'+siteRoot+'img/models/gallery/LYRIC-'+newID+'.jpg" id="'+newID+'" class="zoom" />').appendTo('#modelGallery');
			addpowerzoomInit();
		});
		
		$('#modelNav a').click(function(e){
			e.preventDefault();
			var mnID = $(this).attr('id').substring(1);
			if (!$(this).hasClass('On')) {
				$('#modelNav a').removeClass('On');
				$(this).addClass('On');
				$('#modelNavContent > div').removeClass('On').fadeOut(200);
				$('#'+mnID).addClass('On').fadeIn(400);
				shadowsInit();
			}
		});
		
		$('.photoGallery .description a.more').click(function(){
			if ($(this).hasClass('Open')) {
				$(this).removeClass('Open').text('View Gallery').closest('.photoGallery').find('.photos').slideUp(200);
			}
			else {
				$(this).addClass('Open').text('Hide Gallery').closest('.photoGallery').find('.photos').slideDown(300);
			}
		});
		
		$('.dealerNews .description a.more,.news .description a.more').click(function(){
			if ($(this).hasClass('Open')) {
				$(this).removeClass('Open').text('Expand Story').prev('.newsContent').children('p:not(:first-child)').slideUp(200);
			}
			else {
				$(this).addClass('Open').text('Collapse Story').prev('.news').children('p').slideDown(300);
			}
		});
		
		$('a.addItem').click(function(e){
			e.preventDefault();
			var $lastFS = $('fieldset').last().clone();
			var nxtIndx = $('fieldset').length;
			$lastFS.append('<div class="close" />');
			$lastFS.find('input.itemName').attr('name','oItemName'+nxtIndx);
			$lastFS.find('input.itemNumber').attr('name','oitemNumber'+nxtIndx);
			$lastFS.find('input.quantity').attr('name','oQuantity'+nxtIndx);
			$lastFS.insertAfter($('fieldset').last());
		});
		
		$('.close').live('click',function(){
			$this = $(this);
			$this.parent().fadeOut(200,function(){
				if ($this.parent('fieldset')) {
					$this.parent('fieldset').remove();
				}
			});
			if ($this.parent().hasClass('modUp')||$this.parent().hasClass('modUp2')) {
				var modID = $this.parent().attr('id').substring(6);
				$('a.modPop#m'+modID+',a.modPop2#m'+modID).removeClass('On').find('img').fadeOut(200).remove();
			}
		});
		
		$("form#brandForm").validate({
			debug: true,
			errorElement: "em",
			rules: {
				barName:		{ required:true },
				barPosition:	{ required:true },
				barEmail:		{ required:true, email:true },
				barPhone:		{ required:true }
			},
			messages: {
				barName:		{ required:'Required' },
				barPosition:	{ required:'Required' },
				barEmail:		{ required:'Required', email:'Invalid' },
				barPhone:		{ required:'Required' }
			},
			errorPlacement: function(error, element) {
				error.appendTo( element.prev() );
			},
			success: function(label) {
				label.text("ok!").addClass("success");
			},
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: ".successBrand",
					resetForm: true
				});
			}
		});
		
		$("form#dealerRequestForm").validate({
			debug: true,
			errorElement: "em",
			rules: {
				daName:		{ required:true },
				daPosition:	{ required:true },
				daEmail:	{ required:true, email:true },
				daPhone:	{ required:true }
			},
			messages: {
				daName:		{ required:'Required' },
				daPosition:	{ required:'Required' },
				daEmail:	{ required:'Required', email:'Invalid' },
				daPhone:	{ required:'Required' }
			},
			errorPlacement: function(error, element) {
				error.appendTo( element.prev() );
			},
			success: function(label) {
				label.text("ok!").addClass("success");
			},
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: ".successDealerRequest",
					resetForm: true
				});
			}
		});
		
		$("form#dealerAuthForm").validate({
			debug: true,
			errorElement: "em",
			rules: {
				daPassword:	{ required:true },
			},
			messages: {
				daPassword:	{ required:'Required' }
			},
			errorPlacement: function(error, element) {
				error.appendTo( element.prev() );
			},
			submitHandler: function(form) {
				jQuery(form).ajaxSubmit({
					target: ".successDealerAuth",
					resetForm: true
				});
			}
		});
		
		$.supersized({
		
			//Functionality
			slideshow               :   1,		//Slideshow on/off
			autoplay				:	0,		//Slideshow starts playing automatically
			start_slide             :   0,		//Start slide (0 is random)
			random					: 	1,		//Randomize slide order (Ignores start slide)
			slide_interval          :   5000,	//Length between transitions
			transition              :   1, 		//0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
			transition_speed		:	500,	//Speed of transition
			new_window				:	1,		//Image links open in new window/tab
			pause_hover             :   0,		//Pause slideshow on hover
			keyboard_nav            :   0,		//Keyboard navigation on/off
			performance				:	2,		//0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
			image_protect			:	1,		//Disables image dragging and right click with Javascript
			image_path				:	siteRoot+'img/supersized/', //Default image path

			//Size & Position
			min_width		        :   0,		//Min width allowed (in pixels)
			min_height		        :   0,		//Min height allowed (in pixels)
			vertical_center         :   1,		//Vertically center background
			horizontal_center       :   1,		//Horizontally center background
			fit_portrait         	:   1,		//Portrait images will not exceed browser height
			fit_landscape			:   0,		//Landscape images will not exceed browser width
			
			//Components
			navigation              :   1,		//Slideshow controls on/off
			thumbnail_navigation    :   0,		//Thumbnail navigation
			slide_counter           :   1,		//Display slide numbers
			slide_captions          :   1,		//Slide caption (Pull from "title" in slides array)
			slides 					:  	[		//Slideshow Images
				{ image : siteRoot+'img/backgrounds/b01.jpg' },
				{ image : siteRoot+'img/backgrounds/b02.jpg' },
				{ image : siteRoot+'img/backgrounds/b03.jpg' },
				{ image : siteRoot+'img/backgrounds/b04.jpg' },
				{ image : siteRoot+'img/backgrounds/b05.jpg' },
				{ image : siteRoot+'img/backgrounds/b06.jpg' },
				{ image : siteRoot+'img/backgrounds/b07.jpg' },
				{ image : siteRoot+'img/backgrounds/b08.jpg' },
				{ image : siteRoot+'img/backgrounds/b09.jpg' },
				{ image : siteRoot+'img/backgrounds/b10.jpg' },
				{ image : siteRoot+'img/backgrounds/b11.jpg' },
				{ image : siteRoot+'img/backgrounds/b12.jpg' },
				{ image : siteRoot+'img/backgrounds/b13.jpg' },
				{ image : siteRoot+'img/backgrounds/b14.jpg' }
			]
										
		}); 
		
		$('.bgWhite').each(function(){
			$('<div class="bgShadow"></div>').insertBefore(this);
		});
		shadowsInit();
		
		$('<span class="mask" />').appendTo('h1').width($('h1').width());

		$('body').height($('#center .bgWhite').outerHeight() + 200);
		
		$('input,textarea').defaultValue();
		
/*
		$('#faqs p').hide();
		$('#faqs h6').css({ cursor: 'pointer' }).click(function(){
			if ($(this).hasClass('Open')) {
				$(this).removeClass('Open').next('p').slideUp(250);
			}
			else {
				$(this).addClass('Open').next('p').slideDown(500);
			}
		});
*/
		
		if ($('img.zoom').length) addpowerzoomInit();
		
	});


	$(window).bind("load", function() {
		
		var imgLink = $('#supersized a:eq(1) img').attr('src');
		var imgIndx = imgLink.lastIndexOf("/");
		var imgFile = imgLink.substr(imgIndx).substring(1);
		var imgHref = "http://lyricmotion.com/?bgi="+imgFile;
		$('#bgiEmail').attr('href','mailto:?body='+imgHref);
		
		openBGI();
	
	});
	

})(window.jQuery);


