
//				console.log('yup1');

//var infowindow = null;

$(document).ready(function(){
	
	if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1)) {
		//nada 
		//$('<meta content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" name="viewport"/>').appendTo('head');
		
	} else {
		//non iphone functions
	}
	
	/*locationName = '';
	markerId = '';
	markerTitle = ''; */
	
	
	GREASE.newsHover();
	GREASE.siteOnWindowResize();
	GREASE.carouselAlpha();
	GREASE.customSelectFields();
	GREASE.getInfoLink();
	GREASE.videoLoad();
	GREASE.swfObject();
	//GREASE.customSelectIconChanger();
	
	CORE.bodyClass();
	CORE.externalLinks();
	CORE.isiPad();
	


}); //end document.ready


/*//////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

	SITE SPECIFIC
	
///////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////*/

var GREASE = {
/*==========================================
	function
============================================*/
	 newsHover: function(){
	
		$('.sectionGamma dt').hover( function(){						  
				$(this).next('dd').css({'color':'yellow'});
		}, function () {
    			$(this).next('dd').css({'color':'white'});
  		});

			
	 },


/*==========================================
	Display on window resize
============================================*/
	 siteOnWindowResize: function(){
		 
		//display change on window width variation
		function adjustStyle(width) {
			
			width = parseInt(width);
			 if ((width < 1100)) {
				$("body").css("backgroundImage", "url(images/bg_body-beta.jpg)");  
			} else {
				$("body").css("background-image", "url(images/bg_body-alpha.jpg)");
			}
		}
		
	
		$(function() {
			adjustStyle($(this).width());
			
			$(window).resize(function() {
				adjustStyle($(this).width());
			});
			
		});

	 },
	 

/*==========================================
	carouselAlpha
============================================*/
	 carouselAlpha: function(){
		 
		carouselUl = $('#carouselAlphaContainer ul');
		carouselLi = $('#carouselAlphaContainer li');

		item_marginRight =  carouselLi.css('margin-right');
        item_width = carouselLi.outerWidth() + parseInt(item_marginRight);
         
		liCount = $(carouselLi).size();
		ulWidth = (liCount * item_width) + 10;
		carouselUl.css({width:ulWidth});
		
		//if count 3 (no need to scroll) hide buttons and restore ul pos left to 0, and don't allow last to be moved
		if (liCount < 4){
			$('.carouselAlphaNav').hide();
			$('#carouselAlphaContainer ul').css({'left':'0'});
		} else {
			$('#carouselAlphaContainer ul li:first').before($('#carouselAlphaContainer ul li:last')); 
		}
		
        $('#caNext a').click(function(e){
									  
			var left_indent = parseInt(carouselUl.css('left')) - item_width;
        
            $('#carouselAlphaContainer ul:not(:animated)').animate({'left' : left_indent},500, function(){    
                $('#carouselAlphaContainer ul li:last').after($('#carouselAlphaContainer ul li:first')); 
                carouselUl.css({'left' : '-158px'});
            }); 
			
			e.preventDefault();
        });
        

        $('#caPrev a').click(function(e){

			var left_indent = parseInt(carouselUl.css('left')) + item_width;
			
            $('#carouselAlphaContainer ul:not(:animated)').animate({'left' : left_indent},500,function(){    
				$('#carouselAlphaContainer ul li:first').before($('#carouselAlphaContainer ul li:last')); 
				carouselUl.css({'left' : '-158px'});
            });
			
			e.preventDefault();
            
        });

	},
	
/*==========================================
	customSelectFields
============================================*/
	customSelectFields: function(){
	
			
		// first locate all of the select tags on the page and hide them
		$("select.changeMe").css('display','none');
		//now, for each select box, run this function
		$("select.changeMe").each(function(){
			
			var curSel = $(this);
			// get the CSS width from the original select box
			var gddWidth = $(curSel).css('width');
			var gddWidthL = gddWidth.slice(0,-2);
			var gddWidth2 = gddWidthL - 28;//var gddWidth2 = gddWidthL - 28;
			var gddWidth3 = gddWidthL - 16;//var gddWidth3 = gddWidthL - 16;
			// build the new div structure
			var gddTop = '<div style="width:' + gddWidthL + 'px" class="selectME" tabindex="0"><div class="cornerstop"><div><div></div></div></div><div class="middle"><div><div><div>';
			//get the default selected option
			var whatSelected = $(curSel).children('option:selected').text();
			//write the default
			var gddFirst = '<div class="first clearfix"><span class="selectME gselected" style="width:'+ gddWidth2 +  'px;">'+ whatSelected +'</span><span id="arrowImg"></span></div><ul class="selectME">';
	//		var gddFirst = '<div class="first clearfix"><span class="selectME gselected" style="width:'+ gddWidth2 +  'px;">'+ whatSelected +'</span><span id="arrowImg"></span><div class="clears"></div></div><ul class="selectME">';
			// create a new array of div options from the original's options
			var addItems = new Array();      
			$(curSel).children('option').each( function() {           
				var text = $(this).text();  
				var selVal = $(this).attr('value'); 
				var before =  '<li style="width:' + gddWidthL + 'px;"><a href="#" rel="' + selVal + '" tabindex="0"  style="width:' + gddWidth3 + 'px;">';
				var after = '</a></li>';           
				addItems.push(before + text + after);
			});
			//hide the default from the list of options 
			var removeFirst = addItems.shift();
			// create the end of the div selectbox and close everything off
			var gddBottom ='</ul></div></div></div></div><div class="cornersbottom"><div><div></div></div></div></div>'
			//write everything after each selectbox
			var GDD = gddTop + gddFirst + addItems.join('') + gddBottom;
			$(curSel).after(GDD);
			//this var selects the div select box directly after each of the origials
			var nGDD = $(curSel).next('div.selectME');
			
			$(nGDD).find('li:first').addClass("first");
			
			$(nGDD).find('li:last').addClass('last');
			//handle the on click functions - push results back to old text box
			$(nGDD).click( function(e) {
				 var myTarA = $(e.target).attr('rel');
				 //alert(myTarA)
				 var myTarT = $(e.target).text();
				 var myTar = $(e.target);
				 //console.log(myTarT)
				 //if closed, then open
				 if( $(nGDD).find('li').css('display') == 'none')
					{
							//this next line closes any other selectboxes that might be open
							$('div.selectME').find('li').css('display','none');
							$(nGDD).find('li').css('display','block');
							
							//if user clicks off of the div select box, then shut the whole thing down
							$(document.window || 'body').click( function(f) {
									var myTar2 = $(f.target);
									if (myTar2 !== nGDD) {$(nGDD).find('li').css('display','none');}
							});
									return false;
					}
					else
					{      
							if (myTarA == null){
								$(nGDD).find('li').css('display','none');
										return false;
									}
									else {
										//set the value of the old select box
										$(curSel).val(myTarA);
										
										//set the text of the new one
										 $(nGDD).find('span.gselected').text(myTarT);
										 
										 $(nGDD).find('li').css('display','none');
										 return false;
									}
					}
			//handle the tab index functions
			 }).focus( function(e) {        
						
		
				 $(nGDD).find('li:first').addClass('currentDD');
				 $(nGDD).find('li:last').addClass('lastDD');
				 function checkKey(e){
					//on keypress handle functions
					function moveDown() {
						var current = $(nGDD).find('.currentDD:first');
						var next = $(nGDD).find('.currentDD').next();
						if ($(current).is('.lastDD')){
						return false;
						} else {
							$(next).addClass('currentDD');
							$(current).removeClass('currentDD');
						}
					}
					function moveUp() {
						var current = $(nGDD).find('.currentDD:first');
						var prev = $(nGDD).find('.currentDD').prev();
						if ($(current).is('.first')){
						return false;
						} else {
							$(prev).addClass('currentDD');
							$(current).removeClass('currentDD');
						}
					}
					var curText = $(nGDD).find('.currentDD:first').text();
					var curVal = $(nGDD).find('.currentDD:first a').attr('rel');
											alert(curVal)
				   switch (e.keyCode) {
						case 40:
							$(curSel).val(curVal);
							$(nGDD).find('span.gselected').text(curText);
							moveDown();
							return false;
							break;
						case 38:
							$(curSel).val(curVal);
							$(nGDD).find('span.gselected').text(curText);
							moveUp();
							return false;
							break;
						case 13:
							$(nGDD).find('li').css('display','none');
							}     
				}
				$(document).keydown(checkKey);	
			}).blur( function() {
					$(document).unbind('keydown');
			});
		});
		


	},


/*==========================================
	getInfoLink
============================================*/

	getInfoLink: function() {
	
		$('ul.selectME li a').click(function(){
			
			id = $(this).attr('rel');
			$('.getInfoButton').attr('href', id);
					
		});
						
	},
	
/*==========================================
	customSelect icon changer
============================================*/
	//customSelectIconChanger: function(){
//		
//	 function displayVals() {
//		  var locationValues = $("#locationSelector").val();
//		  
//		  //var multipleValues = $("#multiple").val() || [];
//		  
//		  $("p.displayVal").html("<b>Single:</b> " + locationValues);
//			}
//		
//		$("select").change(displayVals);
//		displayVals();
//
//	}


/*==========================================
	videoLoad
============================================*/
	videoLoad: function(){
		
		var Playlist = function(instance, playlist, options) {
			var self = this;
	
			this.instance = instance; // String: To associate specific HTML with this playlist
			this.playlist = playlist; // Array of Objects: The playlist
			this.options = options; // Object: The jPlayer constructor options for this playlist
	
			this.current = 0;
	
			this.cssId = {
				jPlayer: "jquery_jplayer_",
				interface: "jp_interface_",
				playlist: "jp_playlist_"
			};
			this.cssSelector = {};
	
			$.each(this.cssId, function(entity, id) {
				self.cssSelector[entity] = "#" + id + self.instance;
			});
	
			if(!this.options.cssSelectorAncestor) {
				this.options.cssSelectorAncestor = this.cssSelector.interface;
			}
	
			$(this.cssSelector.jPlayer).jPlayer(this.options);
	
			$(this.cssSelector.interface + " .jp-previous").click(function() {
				self.playlistPrev();
				$(this).blur();
				return false;
			});
	
			$(this.cssSelector.interface + " .jp-next").click(function() {
				self.playlistNext();
				$(this).blur();
				return false;
			});
		};
	
		Playlist.prototype = {
			displayPlaylist: function() {
				var self = this;
				$(this.cssSelector.playlist + " ul").empty();
				for (i=0; i < this.playlist.length; i++) {
					var listItem = (i === this.playlist.length-1) ? "<li class='jp-playlist-last'>" : "<li>";
					listItem += "<img id='" + this.cssId.playlist + this.instance + "_item_" + i +"' src='"+ this.playlist[i].thumb +"' alt='"+ this.playlist[i].name +"' /><div class='videoPlaylistContent'><h3>"+ this.playlist[i].name +"</h3><p class='videoDescription'>"+ this.playlist[i].description +"</p><p class='time'>"+ this.playlist[i].time +"</p><p class='videoDetail'><span>by:</span> "+ this.playlist[i].by +"</p><p class='videoDetail'><span>on:</span> "+ this.playlist[i].on +"</p></div>";
					
					
					// Create links to free media
					if(this.playlist[i].free) {
						var first = true;
						listItem += "<div class='jp-free-media'>(";
						$.each(this.playlist[i], function(property,value) {
							if($.jPlayer.prototype.format[property]) { // Check property is a media format.
								if(first) {
									first = false;
								} else {
									listItem += " | ";
								}
								listItem += "<a id='" + self.cssId.playlist + self.instance + "_item_" + i + "_" + property + "' href='" + value + "' tabindex='1'>" + property + "</a>";
							}
						});
						listItem += ")</span>";
					}
	
					listItem += "</li>";
	
					// Associate playlist items with their media
					$(this.cssSelector.playlist + " ul").append(listItem);
					$(this.cssSelector.playlist + "_item_" + i).data("index", i).click(function() {
						var index = $(this).data("index");
						if(self.current !== index) {
							self.playlistChange(index);
						} else {
							$(self.cssSelector.jPlayer).jPlayer("play");
						}
						$(this).blur();
						return false;
					});
	
					// Disable free media links to force access via right click
					if(this.playlist[i].free) {
						$.each(this.playlist[i], function(property,value) {
							if($.jPlayer.prototype.format[property]) { // Check property is a media format.
								$(self.cssSelector.playlist + "_item_" + i + "_" + property).data("index", i).click(function() {
									var index = $(this).data("index");
									$(self.cssSelector.playlist + "_item_" + index).click();
									$(this).blur();
									return false;
								});
							}
						});
					}
				}
			},
			playlistInit: function(autoplay) {
				if(autoplay) {
					this.playlistChange(this.current);
				} else {
					this.playlistConfig(this.current);
				}
			},
			playlistConfig: function(index) {
				$(this.cssSelector.playlist + "_item_" + this.current).removeClass("jp-playlist-current").parent().removeClass("jp-playlist-current");
				$(this.cssSelector.playlist + "_item_" + index).addClass("jp-playlist-current").parent().addClass("jp-playlist-current");
				this.current = index;
				$(this.cssSelector.jPlayer).jPlayer("setMedia", this.playlist[this.current]);
			},
			playlistChange: function(index) {
				this.playlistConfig(index);
				$(this.cssSelector.jPlayer).jPlayer("play");
			},
			playlistNext: function() {
				var index = (this.current + 1 < this.playlist.length) ? this.current + 1 : 0;
				this.playlistChange(index);
			},
			playlistPrev: function() {
				var index = (this.current - 1 >= 0) ? this.current - 1 : this.playlist.length - 1;
				this.playlistChange(index);
			}
		};
	
	
		var mediaPlaylist = new Playlist("1", [
			{
				name:"Beauty School Dropout",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v:"../flash/assets/video/m4v/beauty-school-dropout.m4v",
				ogv:"../flash/assets/video/ogv/beauty-school-dropout.ogv",
				thumb: "images/photos/img_video-beauty-school-dropout-2010.jpg",
				poster:"images/photos/img_video-beauty-school-dropout-2010-lge.jpg",
				time: "01:11"
			},
			{
				name:"Grease Is The Word",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v: "../flash/assets/video/m4v/grease-is-the-word.m4v",
				ogv: "../flash/assets/video/ogv/grease-is-the-word.ogv",
				thumb: "images/photos/img_video-grease-is-the-word-2010.jpg",
				poster: "images/photos/img_video-grease-is-the-word-2010-lge.jpg",
				time: "01:04"
			},
			{
				name:"Greased Lightnin'",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v: "../flash/assets/video/m4v/greased-lightnin.m4v",
				ogv: "../flash/assets/video/ogv/greased-lightnin.ogv",
				thumb: "images/photos/img_video-greased-lightning-2010.jpg",
				poster: "images/photos/img_video-greased-lightning-2010-lge.jpg",
				time: "01:30"
			},
			{
				name:"You're The One That I Want",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v: "../flash/assets/video/m4v/youre-the-one-that-i-want.m4v",
				ogv: "../flash/assets/video/ogv/youre-the-one-that-i-want.ogv",
				thumb: "images/photos/img_video-one-that-i-want-2010.jpg",
				poster: "images/photos/img_video-one-that-i-want-2010-lge.jpg",
				time: "00:48"
			},
			{
				name:"Summer Nights",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v:"../flash/assets/video/m4v/summer-nights.m4v",
				ogv:"../flash/assets/video/ogv/summer-nights.ogv",
				thumb: "images/photos/img_video-summer-nights-2010.jpg",
				poster:"images/photos/img_video-summer-nights-2010-lge.jpg",
				time: "01:08"
			},	
			{
				name:"We Go Together",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v: "../flash/assets/video/m4v/we-go-together.m4v",
				ogv: "../flash/assets/video/ogv/we-go-together.ogv",
				thumb: "images/photos/img_video-we-go-together-2010.jpg",
				poster: "images/photos/img_video-we-go-together-2010-lge.jpg",
				time: "00:58"
			}
	
		], {
			ready: function() {
				mediaPlaylist.displayPlaylist();
				mediaPlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
			},
			ended: function() {
				mediaPlaylist.playlistNext();
			},
			swfPath: "js",
			solution: "flash, html",
			supplied: "ogv, m4v, oga, mp3"
		});
	
		var homePlaylist = new Playlist("2", [
			{
				name:"Cast 2010",
				description:"Performed by 2010 London cast at the Piccadilly Theatre",
				by: "Grease the musical",
				on: "2010",
				m4v: "../flash/assets/video/m4v/cast-2010.m4v",
				ogv: "../flash/assets/video/ogv/cast-2010.ogv",
				thumb: "images/photos/img_video-cast-2010.jpg",
				poster: "images/photos/img_video-cast-2010-lge.jpg",
				time: "02:07"
			}	
		], {
			ready: function() {
				homePlaylist.displayPlaylist();
				homePlaylist.playlistInit(false); // Parameter is a boolean for autoplay.
			},
			/*ended: function() {
				homePlaylist.playlistNext();
			},*/
			swfPath: "js",
			solution: "flash, html",
			supplied: "ogv, m4v, oga, mp3"
		});
	
	},
	
/*====================================
	Home
======================================*/
	swfObject: function(){
		
		$('#header').flash({
				swf: 'flash/grease-navpanel.swf',
				width: 860,
				height: 350,
				play: true,
				bgcolor: '#ff009a',
				wmode: 'opaque',
				menu: false,
				flashvars: {
					VideoName: 'assets/video/' + whichVideo + '.flv'
				}
			});

	}
	

} //end GREASE




/*///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

	CORE FUNCTIONS
	
	bodyClass
	externalLinks
	toolTip
	is iPad?

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////*/

var CORE = {

/*==========================================
	Body class
============================================*/
	 bodyClass: function(){
		$('body').removeClass('noJs');
		$('body').addClass('jsEnabled');
	 },

/*==========================================
	External links
============================================*/

	 externalLinks: function(){
		 
		$('a[rel="external"]').click( function(e){
			var _externalLinks = this;
			window.open( $(_externalLinks).attr('href') );																
			e.preventDefault();
		});

	},//end externalLinks

	
/*==========================================
	is iPad?
============================================*/
	
	isiPad: function(){
		
		var isiPad = navigator.userAgent.match(/iPad/i) != null;
		var orientation = Math.abs(window.orientation) == 90 ? 'landscape' : 'portrait';
		
		if (isiPad && (orientation = 'portrait')){		
			
			$('#iPhoneViewport').remove();
			//$('#iPadViewport').remove();

			//alert('portrait')
			//$('<meta id="iPadViewport" content="width=device-width; initial-scale=0.75; maximum-scale=1.0;" name="viewport"/>').appendTo('head');
			$('<link href="css/ipad.css" type="text/css" rel="stylesheet" media="screen" />').appendTo('head');
		/*} else if (isiPad) {
		
			$('<meta id="iPadViewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" name="viewport"/>').appendTo('head');*/
		
		}
		
		
	}
	

}//end CORE
