// JavaScript Document

window.onresize = resizeEvents;

var viewportWidth = 0;
var viewportHeight = 0;

function resizeEvents() {
	checkViewport();
	rePosContent();
}

function rePosContent() {
	var contentWidth = $('#overlayContentBox').width();
	var centerContent = (viewportWidth/2) - (contentWidth/2);
	$('#overlayContentBox').animate({marginLeft:centerContent+'px'});
	
	//console.log(viewportWidth);
}

/// NEW FOR HASH ///
function contentHash() {
	
	var Page = 'Home';
	
	if(location.hash != '') {
		Page = location.hash;
		Page = Page.replace('#','');
	}
	
	showPageContent(Page);
	
	/*if($.browser.msie) {
		if($('#historyIframe') != null) {
			alert('iframe');
			$('<iframe id="historyIframe" style="width:98%; height:300px;"></iframe').appendTo('body');
			$('#historyIframe').attr('src','_' + Page + '.cfm?iframeDie=1');
			$("#historyIframe").load(function (){
				//alert('loaded');
				showPageContent(Page);
			});
		}
	}*/
}

//$(window).bind('onhashchange',contentHash);
window.onhashchange = contentHash;
/*if ("onhashchange" in window) {
    alert("The browser supports the hashchange event!");
}*/



$(document).ready(function() {
//function PageScripts() {
	checkViewport();
	//$('#pageContent #content_home').css('display','block');
	prettyPhotoGo();
	addHide();
	$('.preloadFinish').bind('load',function(){
		$('#splashLogoBox').fadeIn(1000);
	});
	
	var outputText = $('#pageTitle').html();
	var className = 'pageTitles';
	$('#pageTitle').html(outputText);
	metrics = textMetrics(outputText, $('#pageTitle'));
	var className = 'pageTitles';
	var imgWidth = metrics.Width;
	
	pageNameImg = createRenderedText(outputText,imgWidth,className,undefined);
	
	$('#pageTitle').html(pageNameImg);
	
	//playVideo();
	
	contentHash();
	
//}
});

/*$(document).ready(function() {
    PageScripts();
	//console.log('doc ready');
});*/

function checkViewport() {
	viewportWidth = $(window).width();
	viewportHeight = $(window).height();
}

function switchHead(obj,position) {
	var headObj = '#' + obj;
	$('.sargeHead').css('display','none');
	$(headObj).css('display','block');
	
	var menuObj = '#menu' + position + ' img';
	
	$('.menuItem img').css({top:'0px'});
	$(menuObj).css({top:'-230px'});
	
	//$(menuObj).animate({top:'-230px'});
	
	/*$(menuObj).fadeTo(100,0.5,function() {
		$(menuObj).css({top:'-230px'});
		$(menuObj).fadeTo(100,1);
	});*/
}

function testEI() {
	alert('hi');
}


/*function pauseVideo() {
	var player2 = document.getElementById('tvMediaPlayer_2');
	player2.flash_pauseVideo();
}*/

var videoSrc = '';
var currentVideo = '';
var player2;

function playVideos(videoSrc) {
	
	if(videoSrc == currentVideo) {
		return false;
	} else {
		currentVideo = videoSrc;
	}
	
	if(videoSrc == ''){
		videoSrc = '/videos/flv/buzz/AllenOnSarge.flv';
	}
	
	//videoSrc = '&file=' + src;
	//console.log('check');
	//var image = '&image=/videos/flv/AllenOnSarge.jpg';
	//var mediaPlayer = '/mediaplayer.swf?width=500&amp;height=345' + src + image;
	
	player2 = document.getElementById('tvMediaPlayer_2');
	if(player2) {
		//alert('exists');
		//player2.flash_changeVideo(videoSrc);
		//$('#gddflvplayer_container').fadeOut(400,function(){
			$('#gddflvplayer_container').load('testVid.cfm?videoUrl=' + videoSrc,function(){
				//$('#gddflvplayer_container').fadeIn(10);
				//$('#gddflvplayer_container').fadeIn(2000,function(){
					//player2.play();
				//});
			});
		//});
	} else {
		//alert(videoSrc);
		var confirmed = confirm('Flash Video Player not detected. Play video in your external player?');
		if(confirmed) {
			location.href = videoSrc;
		}
	}
	/*var player = document.getElementById('tvMediaPlayer');
	player.sendEvent("stop","true");
	player.sendEvent("load",videoSrc);
	player.sendEvent("PLAY","true");*/
	
	//$.prettyPhoto.open(mediaPlayer,'','');
	
//s1.addParam("allowfullscreen","true");
//s1.addParam("allowScriptAccess","true");
//s1.addVariable("width","480");
//s1.addVariable("height","380");
//s1.addVariable("bufferlength","8");
	
}

function returnToGallery() {
	runPrettyPhoto('ccc',1);
}

function externalGalleryCall(src,mediaType) {
	
	//console.log(src);
	
	if(mediaType == 'video') {
		playVideos();
		return;
	}
	
	$.prettyPhoto.open(src,'<span style="font-size:10px; cursor:pointer" onclick="returnToGallery();">return to gallery</span>','');
	
}

function customizeIframe() {

	var cObj = $('#pp_full_res'); //affecting div containing iframe instead
	
	var iframeObj = $('#pp_full_res iframe');
	
	//alert(iframeObj);
	
	//cObj.css('background-image','url(/images/imgLoader.gif)');
	cObj.css('background-repeat','no-repeat');
	cObj.css('background-position','25px 8px');
	//obj.css('border','1px solid red');
	
	iframeObj.bind('load',function(){
		alert('gooo');
		//$(this).parent().css('background','none');
	});
	
}

function runPrettyPhoto(src,iframe) {
	if(iframe == 1) {
		$.prettyPhoto.open('/media.cfm?iframe=true&width=880&height=350','Media','');
	} else {
		$.prettyPhoto.open('/media.cfm?iframe=true&width=880&height=350','Media','');
	}
	//customizeIframe();
	//setTimeout(customizeIframe,100);
	//$.prettyPhoto.open('http://www.youtube.com/watch?v=-GMqDCnxv6w','Media','');
	//$.prettyPhoto.changePage('next');
	//$.prettyPhoto.changePage('previous');
	//$.prettyPhoto.close();
}

var ajaxCall_1 = '';

//for pagebody view

var currentPage = 'Home';

function showPageContent(pageName,queryStr) {
	
	if(pageName == currentPage) {
		return false;
	} else {
		currentPage = pageName;
		if(pageName != 'Home') {
			$('#link_' + pageName).mouseover();
		}
	}
	
	var exrtaText = '';
	
	//put scroll to here
	
	//alert(pageName);
	
	if(pageName == undefined) {
		pageName = 'Home';
		//alert(pageName);
	}
	
	if(queryStr == undefined) {
		queryStr = '';
	}
	
	if(pageName == "Videos") {
		//exrtaText = '<span class="pageTitle_extraText" style="padding-bottom:4px;"> - People Love Sarge</span>';
		exrtaText = queryStr.replace('?catID=','');
		
		//console.log(exrtaText);
	}
	
	
	var outputText = pageName.toUpperCase();
	var className = 'pageTitles';
	//$('#pageTitle').html(outputText);
	
	outputText = outputText.replace('-',' ');
	
	metrics = textMetrics(outputText, $('#pageTitle'));
	var className = 'pageTitles';
	var imgWidth = metrics.Width;	
	pageNameImg = createRenderedText(outputText,imgWidth);
	//$('#pageTitle').html(pageNameImg + exrtaText);
	//////////////$('#pageTitle').html(pageNameImg);
	//$('title').html('Sarge Entertainment - ' + pageName);
	
	$('#pageBody').fadeOut(400,function(){
		
		var loaderIMG = '<img src="/images/loader_yellow_onBlack.gif">';
		$('#pageBody').html(loaderIMG);
		$('#pageBody').css({display:'block'});
		var pageNameCFM = '_' + pageName + '.cfm' + queryStr;
		
		/*if(ajaxCall_1 != '') {
			ajaxCall_1.ajaxStop();
		}*/
		
		//ajaxCall_1 = $.get(pageNameCFM, function(){
		ajaxCall_1 = $('#pageBody').load(pageNameCFM, function(){
			//$('#pageBody').html(ajaxCall_1);
			$('#pageBody').css({display:'none'});
			$('#pageBody').fadeIn(400);
			prettyPhotoGo();
			
			$('#pageTitle').html(pageNameImg);
			
			/*if($.browser.msie) {
				$('#historyIframe').attr('src', pageNameCFM + '?iframeDie=1' + '#' + pageName);
			}*/
			
		});
		
	});
	
}

function textMetrics(curText, styledObj){
	/*tempID = 'tempSpan22';
	$('body').append('<span id="' + tempID + '"></span>');
	$('#' + tempID).html(curText);
	$('#' + tempID).css('font-size', styledObj.css('font-size'));
	$('#' + tempID).css('font-style', styledObj.css('font-style'));
	$('#' + tempID).css('font-weight', styledObj.css('font-weight'));
	$('#' + tempID).css('font-family', styledObj.css('font-family'));
	$('#' + tempID).css('line-height', styledObj.css('line-height'));
	$('#' + tempID).css('text-transform', styledObj.css('text-transform'));
	$('#' + tempID).css('letter-spacing',styledObj.css('letter-spacing'));
	$('#' + tempID).hide();
	var metrics = {
		Width: $('#' + tempID).width(),
		Height: $('#' + tempID).height(),
		lineHeight: $('#' + tempID).css('line-height').split('px')[0],
		fontSize: styledObj.css("font-size").split('px')[0]
	}
	return metrics;*/
	return 200;
}


function createRenderedText(outputText,imgWidth,className,fontFace){
	
	//textMetrics(text, styledObj);
	
	if (imgWidth == undefined) imgWidth = 220;
	if (className == undefined) className = "renderedImg";
	if (fontFace == undefined) fontFace = "helveticaRoundedBlack";
	outputText = outputText.replace("'","%27");
	renderedImg = '<img title="' + unescape(outputText) + '" alt="' + unescape(outputText) + '" class="' + className + '" src="/modules/LocalCFImageText.cfm?LINEHEIGHT=25&imgHeight=24&fontSize=22&amp;OUTPUTTEXT=' + escape(outputText) + '&amp;FONTFACE=' + fontFace + '&amp;FONTCOLOR=ffc000&imgWidth=' + imgWidth + '"/>';
	return renderedImg;
}


//for overlay view
function slideContent(contentSource) {
	checkViewport();
	var contentWidth = $('#overlayContentBox').width();
	var centerContent = (viewportWidth/2) - (contentWidth/2);
	if($('#overlayContentBox').css('marginLeft') == (centerContent + 'px')){
		//console.log('send out');
		$('#overlayContentHTML').fadeOut(function(){

		$('#overlayContentBox').animate({marginLeft:(contentWidth*-1)+'px'},function(){
			$('#overlayContentBox').css({marginLeft: viewportWidth});
			changeContent(contentSource);
			$('#overlayContentHTML').css({display:'none'});
			$('#overlayContentBox').animate({marginLeft:centerContent+'px'},function() {
				$('#overlayContentHTML').fadeIn();
			});
		});
		
		});
	} else {
		//console.log('no send out');
		//console.log($('#overlayContentBox').css('marginLeft') + ' - ' + centerContent + 'px');
		changeContent(contentSource);
		//$('#overlayContentBox').animate({marginLeft:centerContent+'px'});
		$('#overlayContentHTML').css({display:'none'});
		$('#overlayContentBox').animate({marginLeft:centerContent+'px'},function() {
			$('#overlayContentHTML').fadeIn();
		});
	}
}

/// Here Change Content ///
function changeContent(contentSource) {
	
	var loaderIMG = '<img src="/images/loader_yellow_onBlack.gif">';
	$('#overlayContentHTML').html(loaderIMG);
	var pageNameCFM = '_' + contentSource + '.cfm';
	//$('#overlayContentHTML').load(pageNameCFM);
	$('#overlayContentHTML').load(pageNameCFM, function(){
														
		//$('#overlayContentHTML').css({display:'none'});
		//$('#overlayContentHTML').fadeIn();
		prettyPhotoGo();
		
	});
	//$('#pageTitle').html(pageName);
	//$('title').html('Sarge Entertainment - ' + pageName);
}
function showOverlay(contentSource) {
	$('#overlayContentBox').css({marginLeft: viewportWidth});
	$('#contentOverlay').fadeIn(700,function() {
		$('#contentMenu').animate({"top": "+=25px"}, "medium", function() {
			slideContent(contentSource);
			//slideInContent();
		});
	});
}
function hideOverlay() {
	$('#contentMenu').animate({"top": "-=25px"}, "fast",function() {
		$('#contentOverlay').fadeOut(300);
	});
	
	$('#pages div').css('display','none');
}



//////////////////////////////////////////

function playVideo() {
	//var player = document.getElementById('tvMediaPlayer');
	//player.sendEvent("PLAY","true");
	
	alert('hi');
	
	var player2 = document.getElementById('tvMediaPlayer_2');
	player2.sendEvent("PLAY","true");
	
}

var loadImgCount = 0;
var loadImgArray = new Array();

var contentShown = false;

function showContent(skipLoad) {
	
	if(contentShown == true) {
		//console.log('escaped');
		return;
	}
	
	loadImgCount = loadImgCount + 1;
	
	//console.log(loadImgCount + ' - ' + loadImgArray.length);
	
	//console.log(Math.floor((100/loadImgArray.length)*loadImgCount) + '%');
	var percentLoaded = Math.floor((100/loadImgArray.length)*loadImgCount) + '%';
	$('#percentLoaded').html(percentLoaded);
	
	if(loadImgCount == loadImgArray.length || skipLoad == true) {
		
		$('#splashLogoBox').fadeOut(1000,function(){
			if (typeof loadTV == "function") {
			//if(loadTV) {
				loadTV();
			}
			$('#splashLayer').fadeOut(700,function(){
				//$('#pageContent #content_home').fadeIn(1000);
				//$('#mainMenu').fadeIn(1000);
			});
		});
		contentShown = true;
		
		//setTimeout(playVideo,2000);
		
	}
	
}

function addHide() {
	
	//console.log('js ran');
	
	$('#pageContent #content_home').fadeIn(1000);
	
	loadImgArray = $('.hideTillLoaded img');
	
	//console.log(loadImgArray.length);
	
	$(loadImgArray).each(function(){
								  
		//console.log('added');
		
		$(this).bind('load',function(){
			//$(this).css('visibility','visible');
			//$(this).fadeIn(1000);
			//$(this).parent().css('background','none');
			//console.log('img');
			showContent();
		})
	});
}


function prettyPhotoGo() {
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.50, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
		modal: false, /* If set to true, only the close button will close the window */
		changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
		callback: function(){} /* Called when prettyPhoto is closed */
	});
}


//showPageContent('Home');


////// Notes ///

//document size
//var pageWidth = $(document).width();
//var pageHeight = $(document).height();

//window size
//var viewportWidth = $(window).width();
//var viewportHeight = $(window).height();


