// JavaScript Document
$(document).ready(init);

var windowWidth = 400;
var windowHeight = 400;
var startWidth = 0;
var startHeight = 0;
var startTopWidth = 0;
var startTopHeight = 0;
var startLeft = 0;
var startTop = 0;
var startPortWidth = 640;
var startPortHeight = 160;
var portfolioSource = "";
var fLeft = 0
var fTop = 0;
var carouselRight = 0;
var carouselBottom = 0;
var carouselHeight = 0;
var carouselWidth = 0;

var leftColW = 0;
var centerColMarLeft = 0;
var rightColW = 0;
var centerColW = 0;
var centerColTrim = 0;

var eiReady = false;
function init()
{
	jQuery.gaTracker('UA-3857475-7');
	
	$('.siteIndex .top').css({'z-index':2});
	$('.siteIndex #mainNav').css({'z-index':2});
	$('.siteIndex .top *').css({'z-index':2});
	$('.siteIndex #mainNav *').css({'z-index':2});
	
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	startWidth = Math.ceil(windowWidth*0.95);
	startHeight = Math.ceil(windowHeight*0.95);
	startTopWidth = $('div.top').width();
	startTopHeight = $('div.top').height();
	startLeft = Math.ceil((windowWidth - startWidth));
	startTop = (windowHeight - startHeight);
	
	$('.siteIndex #mainContent')
		.width(windowWidth -20)
		.height(windowHeight -20)
		.css({'top': 20,'left':20});
	
	$(window).bind('resize',positionContent).bind('scroll',portfolioScroll);
	
	$('.showPortfolio').click(updatePortfolioSource);
	
	var posRight = 0;
	var setRight = 0;
	var adj = 0;
			
	$("div.portfolio")
		.width(windowWidth)
		.height(windowHeight)
		.css({'left':0,'top':0});
		
	$('.hide').hide();
	
	//apply round corner appearance to blurb boxes
	$("div.blurb")
		.wrap('<div class="dialog"><div class="bd"><div class="c"><div class="s"></div></div></div></div>')
    $('div.dialog')
		.prepend('<div class="hd"><div class="c"></div></div>')
		.append('<div class="ft"><div class="c"></div></div>');
	if(jQuery.support.opacity == false)
	{
		$('div.dialog div').css({'position':'relative','float':'left'});
		$('div.dialog .hd').css({'height':10,'width':200,'float':'left'});
		$('div.dialog .hd .c').css({'float':'right', 'height':10, 'width':10});
		$('div.dialog .ft').css({'height':10,'width':200,'float':'left'});
		$('div.dialog .ft .c').css({'float':'right', 'height':10, 'width':10});
		$('div.dialog .bd .c').css({'width':210});
		$('div.dialog .bd .s').css({'width':190});
	
	}
	if(jQuery.browser.msie && jQuery.browser.version=="6.0")
	{
		$('div.rightCol').css({'margin-left':-300})
	}
	//////////////////////////////	
	
	$('.twitter').clickURL();
	$('.twitter').clickHash();
	$('.twitter').clickAt();
//	
	
	$('a').each(function(i){
			var h=$(this).attr('href');
			if(h.indexOf('http://')>-1 && h.indexOf('get.adobe.com')< 0)
			{
				var cI = h.indexOf('cornett-ims.com');
				if(cI<0 || cI>12)
				{
					$(this).attr({'target':'_blank'});
				}
			}
		})
	$('a').filter('[target=_blank]')
		.each(function(i){
				var targetURL = $(this).attr('href');
				if(targetURL.indexOf('www.twitter.com') < 0)
				{
					while(targetURL.indexOf("#")>-1) targetURL = targetURL.replace('#','%23');
					while(targetURL.indexOf("&")>-1) targetURL = targetURL.replace('&','%26')
					while(targetURL.indexOf("=")>-1) targetURL = targetURL.replace('=','%3D')
					while(targetURL.indexOf("?")>-1) targetURL = targetURL.replace('?','.escQuestionMark.')
					$(this).attr({'href':'/index.php/cornett/newWin/?frameHref='+ targetURL})
				}
			
		});
	
	
	var numCols = 2;
	var centerWidth = $('.centerCol').width();
	var colWidth = centerWidth;
	
	$('.centerCol .columns').columnize();
	$('.centerCol .columns .column:first').addClass('colOne');
	$('.centerCol .columns .column:nth-child(2)').addClass('colTwo');
	$('.centerCol .columns .colOne .block:last p:last').css({'padding-bottom':'0'});
	
	if(centerWidth <= 500)
	{
		var collage = $('#aboutUsCollage').remove();
		$('.colTwo').prepend(collage);
	}


	resetHomePage();

}

function isReady()
{
	eiReady = true;
	return eiReady
}

function sendToJavaScript(identifier)
{
	caseStudyCopyURL = '/caseStudies/htmlFiles/'+ identifier + '.html'
	jQuery.ajax({
		   type:"GET", 
		   url:caseStudyCopyURL,
		   success: function(txt){
			   $('.caseStudy').html(txt)
		   }
	});
	portfolioSource = '/caseStudies/xmlFiles/'+ identifier + '.xml';
	$("#flashPortfolio")
		.html("");
	initResize();
	loadSlideShow();
}

function resetHomePage()
{
	var zAdj = -1;
	if(jQuery.support.opacity)
	{
		zAdj = 0;
	}
	$('.siteIndex #mainContent a.showPortfolio').bind('click',updatePortfolioSource);
	$('.siteIndex #mainContent a.hidePortfolio').bind('click',restoreMain);
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	carouselWidth = windowWidth;
	carouselHeight = $('.siteIndex #mainContent').height() -$('.top').height();
	$('.siteIndex #mainContent')
		.fadeIn(500);
		
	$('div#flashPortfolio')
		.css({'width':carouselWidth,'height':carouselHeight,'right':0, 'bottom':carouselBottom, 'left':'', 'top':''})
		.flashembed({version:[9,0,115], width:'100%',height:'100%',wmode:"transparent",src:"/flash/indexCarousel/carousel.swf", onFail:carouselFail});
		
	$('#brain')
		.css({'width':windowWidth,'height':windowHeight,'left':0, 'top':0, 'z-index':zAdj})
		.flashembed({version:[10,0], width:'100%',height:'100%',wmode:"transparent",src:"/flash/indexBrain/brain.swf", onFail:brain9});
	
	if(jQuery.browser.msie && jQuery.browser.version=="6.0")
	{
		$('#brain').css({'height':carouselHeight, 'top':"", 'bottom':0, 'z-index':3})
	}
	else
	{
		$('#brain *').css({'z-index':zAdj})
	}
}

function brain9()
{
	$('#brain').flashembed({version:[9,0,115], width:'100%',height:'100%',wmode:"transparent",src:"/flash/indexBrain9/brain.swf", onFail:carouselFail});
}

var pOffset = 0;
var pLOffset = 0;
function portfolioScroll(e)
{
	pOffset = ($(window).height() - $('#flashPortfolio').height())/2 +20;
	var s = $(window).scrollTop() + pOffset;
	$('#flashPortfolio').animate({'top':s},200);
}

function carouselFail(version)
{
	windowWidth = $(window).width();
	var newWidth = Math.ceil(windowWidth*0.95);
	var xOffset = Math.round((windowWidth - newWidth));
	if(xOffset < 0) xOffset = 0;
	var pLOffset = xOffset + 260
	var portWidth = windowWidth - pLOffset;
	$('div.portfolio').css({'width':'auto','height':'auto','text-align':'left', 'padding':'20', 'margin-left':pLOffset}).html('<h1>Welcome to Cornett-IMS.com</h1><p>This content requires Flash Player version '+ this.version +'. You have ' + version + '.</p><p><a href="http://get.adobe.com/flashplayer/">Download the latest version here</a>.</p>');
}

function positionContent()
{
	
	windowWidth = $(window).width();
	$('.siteIndex #mainContent').width(windowWidth);
	windowHeight = $(window).height();
	var newWidth = Math.ceil(windowWidth * 0.95);
//	alert('ding');
	var newHeight = Math.ceil(windowHeight * 0.95);
	var newTop = windowHeight - newHeight;
	var newLeft = Math.ceil((windowWidth - newWidth));
	carouselHeight = newHeight -$('.top').height();
	carouselWidth = windowWidth;
	$('div#flashPortfolio')
		.css({'right':0, 'height':carouselHeight, 'width':carouselWidth})
	$('#brain')
		.css({'right':0, 'height':windowHeight, 'width':windowWidth})
	if(!jQuery.support.boxModel)
	{
		$('#brain').css({'height':carouselHeight})
	}

	$('.siteIndex #mainContent').animate({'right':0, 'top':20, 'height':windowHeight-20, 'width':windowWidth-20},500);

	var numCols = 2;
	var centerWidth = $('.centerCol').width();
	var colWidth = centerWidth;
	if(centerWidth <= 500)
	{
		var collage = $('#aboutUsCollage').remove();
		$('.colTwo').prepend(collage);
	}
	return false
}


var caseStudyCopyURL = "/caseStudies/htmlFiles/test.html"

function updatePortfolioSource(e)
{
	caseStudyCopyURL = '/caseStudies/htmlFiles/'+ e.target.id + '.html'
	jQuery.ajax({
		   type:"GET", 
		   url:caseStudyCopyURL,
		   success: function(txt){
			   $('.caseStudy').html(txt)
		   }
	})
	portfolioSource = '/caseStudies/xmlFiles/'+ e.target.id + '.xml'
	$("#flashPortfolio")
		.html("")
	loadSlideShow();
	
	return false
}

function loadSlideShow()
{
	var h = $("#flashPortfolio").html();
	if(h == "")
	{
		$("#flashPortfolio")
		.flashembed({src:'/flash/SlideShow.swf',width:"100%",height:"100%",wmode:"transparent", flashvars:"slideList="+portfolioSource})
	}
}

function initResize(){
	$(window).bind('resize',resizeMain);
	$(window).unbind('resize',positionContent);
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	var newWidth = Math.ceil(windowWidth*0.95);
	var newHeight = Math.ceil(windowHeight*0.95);
	var boxHeight = newHeight - 20;
	var yTrim = $('#mainContent').outerHeight() - $('#mainContent').height()
	var xTrim = $('#mainContent').outerWidth() - $('#mainContent').width()
	var yOffset = Math.round((windowHeight - (newHeight + yTrim)));
	if(yOffset < 0) yOffset = 0;
	var xOffset = Math.round((windowWidth - (newWidth + xTrim)));
	if(xOffset < 0) xOffset = 0;
//	$('#mainContent')
//		.animate({height:newHeight,width:newWidth,top:yOffset,left:xOffset},500);
//	$('div.top')
//		.animate({width:200,height:boxHeight},500)
	var pLOffset = xOffset + 440
	var pHeight = newHeight - $('#mainNav').outerHeight();
	var portWidth = windowWidth - pLOffset;
	$('div.portfolio')
		.animate({width:portWidth,height:pHeight},500,'linear',loadSlideShow)
//	$('div.dialog')
//		.fadeOut(500);
	$('div.caseStudy')
		.css({height:boxHeight})
		.slideDown(500);
	$('ul.portfolioMenu')
		.slideDown(500);
}


var yOffset = 0
function resizeMain(event)
{
	windowWidth = $(window).width();
	windowHeight = $(window).height();
	var newWidth = Math.floor(windowWidth*0.95);
	var newHeight = Math.floor(windowHeight*0.95);
	var boxHeight = newHeight - 20;
	var yTrim = $('#mainContent').outerHeight() - $('#mainContent').height()
	var xTrim = $('#mainContent').outerWidth() - $('#mainContent').width()
	var yOffset = Math.ceil((windowHeight - (newHeight + yTrim)));
	if(yOffset < 0) yOffset = 0;
	var xOffset = Math.ceil((windowWidth - (newWidth + xTrim)));
	if(xOffset < 0) xOffset = 0;
	$('#mainContent')
		.css({top:20,height:windowHeight-20,width:windowWidth-20,right:0});
//	$('div.top')
//		.css({'height':boxHeight})
	var pLOffset = xOffset + 440
	var pHeight = newHeight - $('#mainNav').outerHeight();
	var portWidth = windowWidth - pLOffset;
	$('div.portfolio')
		.css({width:portWidth,height:boxHeight})
//	$('div.dialog')
	$('div.caseStudy')
		.css({height:boxHeight},500)
//	$('ul:has(.hidePortfolio)')

}
function restoreMain(event)
{
	$(window).unbind('resize',resizeMain)
	$(window).bind('resize',positionContent)
//	$('#mainContent')
//		.animate({height:startHeight,width:startWidth},500,'linear',positionContent);
//	$('div.top')
//		.animate({width:startTopWidth,height:startTopHeight},500)
//	$('div.portfolio')
//		.animate({width:startPortWidth,height:startPortHeight},500)
//	$('div.dialog')
//		.fadeIn(500);
	$('div.caseStudy')
		.slideUp(500);
	$('ul.hide')
		.slideUp(500,resetHomePage);
//	$("#flashPortfolio")
//		.flashembed({src:"flash/cornettLogo.swf",width:"640",height:"160",wmode:"transparent"})
	return false
}