var pages = [];
var currentX=0;
var currentY=0;
var lastX=0;
var lastY=0;
var isSliding=false;
var leftMenuArr = []
var mode = ""
var peopleCards;
var canvasWidth;
var canvasHeight;
var prevOrientation;
var hashState = ""
var cssBackground = false;
var newsFunc;
var isPlayingGame = false;
var showCaseMatrix;
var miniGame;
var lang = "";
var isIpadComp = false;
var isWebkitComp = false;
var jqSpaceship;
var jqPagesArr;
var jqLeftmenu;
var $docHeight;
var $docWidth;
var jqShowCase;
var jqCasesAll;
var VM;

/*
FIRST INITIATION
*/

function setUpAll() 
{
	
	
	
	//knockout
	function AppViewModel() {
	   this.newsList= ko.observableArray();
	   this.onNextClick= function(event)
	   {
	   		newsFunc.onNextClick();
	   }
	   this.onPrevClick= function(event)
	   {
	   		newsFunc.onPrevClick();
	   }
	}
	vm= new AppViewModel();
	ko.applyBindings(vm);
	
	//cache jquery selectors
	jqSpaceship = $("#spaceship")
	jqLeftmenu = $('#leftMenu');
	jqShowCase = $('#showcase');
	jqCasesAll =  $("#caseAllScroller");	
	jqPagesArr = new Array()
	$docHeight =  $("body").innerHeight()
	$docWidth =  $("body").innerWidth()	

	//Exchange fonts
	Cufon.replace('h1');
	Cufon.replace('h2');
	Cufon.replace('h3');
	Cufon.replace('#topMenu');
	Cufon.replace('#newsmenu');
	Cufon.replace('#cardClose');
	Cufon.replace('#spaceenemies');
	Cufon.replace('.contacthl');
	Cufon.replace('.splashHeadline');
	
	
	// Bind History handler for ALL hash/state changes
	$.History.bind(function(state){
		historyNavigate(state)
	});

	//Add click events to showreel
	$('#showreelbutton').click(showcaseOpen);
	$("#cases-close").click(showcaseClose);
		
	//If ipad, use webit animations
	if((typeof Touch == "object") && (typeof WebKitTransitionEvent == "object")) {
		isIpadComp = true;
		prevOrientation = orientation
		
		var swipe = new Swipe()
		swipe.callback = slide;		
		
		//Has to do interval to check for hashchange
		hashState = $.History.getHash()
		setInterval(checkforhash, 200)
	}
	
	if( (typeof WebKitTransitionEvent == "object")) {
		isWebkitComp = true;
	}
	
	//Set resize handler	
	$(window).resize(function() {
		resposElements()								  			
	});
	resposElements()
	
	//Check if support of resizing background image
	if(document.body.style.backgroundSize == "")
	cssBackground = true

	//Initiate functions for people
	peopleCards = new PeopleCards(this)
	
	//Initiate functions for news
	newsFunc = new News(this)
	
	//Set mapimg for contact
	$("#mapimg").html('<img src="/_site/img/contactmap.gif" width="590" height="276" />')


	//Bind mode change to this
	$(this).bind("modeChange", function(e, val){
		mode = val										
	})

	//Step through all divs in the page array
	//and change their styles
	$.each(pages,  function(ix,jx){
		$.each(jx,  function(ixx,jxx){			
			if(isWebkitComp) {							 
				pages[ix][ixx].matrix = new WebKitCSSMatrix()			 
				pages[ix][ixx].tx = 0
				pages[ix][ixx].ty = 0
			}
			jqPagesArr[jxx.id] = $(jxx.id)
			$(jxx.id).removeAttr("style")
			$(jxx.id).css({display: "none"})
			$(jxx.id).addClass("page");
		})
	})

	
	//set up topMenu
	$('#topMenu').each(function(nr, topMenu) {		
		var menuArray = getElementsByClass("topMenuLink", this)		
		$.each(menuArray,  function(i,j){
			$(j).mouseover(function() {			
				$(j).addClass("hover");
				Cufon.replace('#topMenu');
			});
			
			$(j).mouseout(function() {
				$(j).removeClass("hover");
				Cufon.replace('#topMenu');
			});
			
			$(j).click(function() {
				getContent(i, 0);
			});
		})
	});
	
	//Set up leftMenus
	for(var i=0; i< pages.length; i++) {
		if(pages[i].length > 1) {			
			leftMenuArr[pages[i][0].id] = ""			
			for(var j=0; j<pages[i].length;j++) {
				var longlabel = $(pages[i][j].id).find('.headline').first().text();
				var ind = j
				var punct = "."
				
				if(ind == 0) {
					ind = "•"
					punct = " "
				}

				leftMenuArr[pages[i][0].id] += ('<div class="leftMenuItem"><div class="itemBox black"><span class="label black" desc="' + (ind) + punct + ' ' + longlabel + '" id="' + i + '-' + j + '" onClick="goLeftNav(\'' + i + '-' + j + '\')">' + (ind) + '</span></div></div>')
			}
		}		
	}

	//Add mouse events for left menu
	var moTimeout;
	if(!isIpadComp) {		
		$('#leftMenu .itemBox .label').live('mouseover', function() {
			$(this).parent().stop()
			$(this).stop()
			$(this).parent().animate({width: "240"}, 100, "easeOutQuad");
			$(this).animate({width: "240"}, 100, "easeOutQuad");
			var labelobj = $(this)
			moTimeout = setTimeout(function() {
				labelobj.empty()
				labelobj.append(labelobj.attr("desc"))
			},100);
		});
	
		$('#leftMenu .itemBox .label').live('mouseout', function() {
			clearTimeout(moTimeout);			
			$(this).parent().stop()
			$(this).stop()
			$(this).parent().animate({width: "24"}, 100, "easeOutQuad");
			$(this).animate({width: "24"}, 100, "easeOutQuad");
			$(this).empty()
			$(this).append($(this).attr("desc").substring(0,1))
		});
	} else {		
		//Set anim props for left menu
		$("#leftMenu")[0].style.webkitTransitionProperty = "-webkit-transform";
		$("#leftMenu")[0].style.webkitTransitionDuration = ".5s";
		$("#leftMenu")[0].style.webkitTransitionTiming = "ease-out";
	}
	
	//Show start page
	$(pages[0][0].id).css({display: "block"});
	markTopMenu(0);
	
	//Splashfunction
	setStartSplash();
			
	if(miniGame == true)
	initSpaceShip(this)
	
	if(lang == "en") {
		if(isIpadComp)
		$("#gameinstructions").append('<img src="img/instr_touch.gif" width="235" height="95" />');
		else
		$("#gameinstructions").append('<img src="img/instr_desktop.gif" width="235" height="95" />');
	} else {
		if(isIpadComp)
		$("#gameinstructions").append('<img src="_site/img/instr_touch.gif" width="235" height="95" />');
		else
		$("#gameinstructions").append('<img src="_site/img/instr_desktop.gif" width="235" height="95" />');
	}
	
	initCases()
	
	if(isWebkitComp) {
		var tranf = window.getComputedStyle($('#showcase')[0]).webkitTransform
		
		//console.log(tranf)
		if(tranf != "none")
		showCaseMatrix = new WebKitCSSMatrix(tranf)

		removeSubMenuItems()
		jqLeftmenu.css({display:"table"})
		jqLeftmenu[0].style.webkitTransitionDuration = ".3s";
		setTimeout(function(){
			var tranf2 = window.getComputedStyle(jqLeftmenu[0]).webkitTransform
			
			if(tranf2 != "none") {
				var leftMatrix = new WebKitCSSMatrix(tranf2)
				//jqLeftmenu[0].style.webkitTransform = leftMatrix.translate(leftMatrix.e - leftMatrix.e + 100)		
				jqLeftmenu[0].style.webkitTransform = leftMatrix.translate( - leftMatrix.e)
			} else {
				var leftMatrix = new WebKitCSSMatrix()
				//jqLeftmenu[0].style.webkitTransform = leftMatrix.translate(leftMatrix.e - leftMatrix.e + 100)		
				jqLeftmenu[0].style.webkitTransform = leftMatrix.translate( - leftMatrix.e)
			}
		},10);
	}	
}

var splashClosed = true;
$(".foldicon").live("click", function(){
	setStartSplash();	
});

function setStartSplash() {

	if(!splashClosed) {
		splashClosed = true;
		$("#splash .textcontent").html($("#splash .secondtext").html())
		$("#splash .foldicon").removeClass("up");
		$("#splash .foldicon").addClass("down");
		
	} else {
		splashClosed = false;
		$("#splash .textcontent").html($("#splash .firsttext").html())
		$("#splash .foldicon").removeClass("down");
		$("#splash .foldicon").addClass("up");
		
	}
}


var gameTimeOut;
function startTheGame() {	
	if($.History.getHash() == "") {
		if(currentX == 0) {
			if(miniGame)
			miniGame.init()
			gameTimeOut = setTimeout(function(){$("#spaceship:hidden:first").fadeIn(1000)}, 2000);
		}
	}	
}

function showGameInfo() {
	$("#gameinstructions").css("visibility", "visible")
}

function hideGameInfo() {
	$("#gameinstructions").css("visibility", "hidden")
}

/*
IPAD HISTORY IS BROKEN, INTERVAL CHECKS FOR HASH CHANGES
*/
function checkforhash() {	
	var hash = $.History.getHash()
	if(hash != hashState){
		hashState = hash
		historyNavigate(hash)
	}
}

/*
FUNCTION FOR NAVIGATING HISTORY WITH HASH
*/
function historyNavigate(pagetitle) {
	
	if(pagetitle == "Mobipong") {
		$('#showreelbutton').hide();
	} else {
		$('#showreelbutton').show();	
	}
	
	for(var i=0; i< pages.length; i++) {
		for(var j=0; j<pages[i].length;j++) {
			if(pages[i][j].title == ("#" + pagetitle)) {
				getContent(i,0)
				break;
			}
		}
	}
}

/*
FUNCTION FOR NAVIGATING IN LEFT NAV
*/
function goLeftNav(idstr) {
	var indexArr = idstr.split("-")										  											  
	getContent(parseInt(indexArr[0]), parseInt(indexArr[1]));
}

/*
MOUSE CLICK OM LEFT NAV, CLIENTS COLUMN IS HARD CONDED
*/
function showClient(id) {
	goLeftNav("3-" + id)
}

function subNav(id) {
	for(var i=0; i< pages.length; i++) {
		for(var j=0; j<pages[i].length;j++) {
			if(pages[i][j].id == id) {
				getContent(i,j)
				break;
			}
		}
	}
}

/*
ON TOP MENU CLICK, GO TO NEW CONTENT PANEL
*/
function getContent(x, y) 
{	
	if(!isSliding && mode == ""){
		lastX=currentX;
		lastY=currentY;		
		currentX=x;
		currentY=y;		
		slide("menuChange");

	}	
}

/*
MARK ITEM IN TOPMENU AS SELECTED
*/
function markTopMenu(menuId)
{
	//set up topMenu
	$('#topMenu').each(function(nr, topMenu) {		
		var menuArray = getElementsByClass("topMenuLink", this)		
		$.each(menuArray,  function(i,j){
			if(i != menuId)  {
				$(j).removeClass("hover");
				$(j).removeClass("selected");		
			} else {
				$(j).addClass("selected");
			}
		})
	});	
	Cufon.replace('#topMenu');
}

/*
MARK ITEM IN LEFT MENU AS SELECTED
*/
function markLeftMenu(selInd) {
	$('#leftMenu .leftMenuItem').each(function(ix){
		if(ix !=  selInd) {
			$(this).children().removeClass("selected")
		} else {
			$(this).children().addClass("selected")
		}
	})
}

/*
SLIDE CONTENT
*/
function slide(dir)
{	
	if(!isSliding && mode == "" && isPlayingGame == false){

		isSliding=true;
		peopleCards.closePeopleCard()		
		var xPosOut="0%";
		var yPosOut="0%"
		
		switch(dir){
			case "right":
				if(currentX>0){
					currentX--;
					currentY=0;
					xPosOut="100%"
				}else {
					isSliding=false;
					return;
				}
				break;
			
			case "left":
				if(currentX<pages.length-1){
					currentX++;
					currentY=0;
					xPosOut="-100%"
				}else {
					isSliding=false;
					return;
				}
				break;
			
			case "down":
				if(currentY>0){
					currentY--;
					yPosOut="100%"
				}else {
					isSliding=false;
					return;
				}
				break;
			
			case "up":
				if(currentY<pages[currentX].length-1){
					currentY++;
					yPosOut="-100%"
				}else {
					isSliding=false;
					return;
				}
				break;
				
			case "menuChange":
				if(lastX<currentX){
					xPosOut="-100%"
				}else {
					xPosOut="100%"
				}
				
				if(lastY<currentY){
					yPosOut="-100%"
				}else {
					yPosOut="100%"
				}
				break;
		}
	
		//sliding between menu-pages----------------------
		if(lastX!=currentX){
	
			if(currentX == 0){
				if(miniGame)
				miniGame.showCanvas()
			} else {
				if(miniGame) {
					miniGame.destroy()				
					jqSpaceship.fadeOut(100)
				}
				
				if(gameTimeOut)
				clearTimeout(gameTimeOut)
			}
			
			removeSubMenuItems()		
			
			markTopMenu(currentX)
			
			var currPage = jqPagesArr[pages[currentX][0].id]
			var prevPage = jqPagesArr[pages[lastX][lastY].id] 
			
			_gaq.push(['_trackPageview', lang + "/" + pages[currentX][0].id]);	
			
			document.location.href = pages[currentX][0].title

			if(isWebkitComp) {							
				currPage.css({display: "block"})	

				var docWidth = $docWidth
				var matrixCurr = pages[currentX][0].matrix
				var matrixPrev = pages[lastX][lastY].matrix

				prevPage[0].style.webkitTransform = matrixPrev
				
				if(lastX>currentX) {
					currPage[0].style.webkitTransform = matrixCurr.translate(-docWidth - matrixCurr.e)	
				} else {
					currPage[0].style.webkitTransform = matrixCurr.translate(docWidth - matrixCurr.e)				
				}
	
				setTimeout(function(){
			
					currPage[0].addEventListener( 'webkitTransitionEnd', menuChangeDone , false ); 
					
					currPage[0].style.webkitTransitionProperty = "all";
					currPage[0].style.webkitTransitionDuration = ".5s";
					currPage[0].style.webkitTransitionTiming = "ease-out";

					prevPage[0].style.webkitTransitionProperty = "all";
					prevPage[0].style.webkitTransitionDuration = ".5s";
					prevPage[0].style.webkitTransitionTiming = "ease-out";
					
					if(lastX>currentX) {
						currPage[0].style.webkitTransform = matrixCurr.translate(0)
						prevPage[0].style.webkitTransform = matrixPrev.translate(docWidth)
						pages[currentX][0].tx =docWidth
						pages[lastX][lastY].tx = docWidth
					} else {
						currPage[0].style.webkitTransform = matrixCurr.translate(0)
						prevPage[0].style.webkitTransform = matrixPrev.translate(-docWidth)
						pages[currentX][0].tx = -docWidth
						pages[lastX][lastY].tx = -docWidth
					}
	
				},1)

			} else {
				if(lastX>currentX){
					currPage.css({left:"-100%", display: "block"})
				}else {
					currPage.css({left:"100%", display: "block"})
				}

				prevPage.animate({left:xPosOut}, 500, "easeOutQuad", menuChangeDone);
				currPage.animate({left: "0%", top:"0%"}, 450, "easeOutQuad");
			}
			
		}
		
		//Sliding between subpages------------------------------------------
		else if(lastY!=currentY){

			markLeftMenu(currentY)
			
			var currPage = jqPagesArr[pages[currentX][currentY].id]
			var prevPage = jqPagesArr[pages[lastX][lastY].id] 
			
			_gaq.push(['_trackPageview', lang + "/" + pages[currentX][currentY].id]);	
			
			document.location.href = pages[currentX][currentY].title
			
			currPage.css({display: "block"});
			prevPage.css({display: "block"});

			if(isWebkitComp) {

				var matrixCurr = pages[currentX][0].matrix
				var matrixPrev = pages[lastX][lastY].matrix
				
				prevPage[0].style.webkitTransform = matrixPrev

				if(lastY > currentY){			
					currPage[0].style.webkitTransform = matrixCurr.translate(0, -$docHeight - matrixCurr.f )					
				}else {
					currPage[0].style.webkitTransform = matrixCurr.translate(0, $docHeight - matrixCurr.f)
				}
				
				setTimeout(function(){

					currPage[0].addEventListener( 'webkitTransitionEnd', subpageChangeDone , false ); 
					
					currPage[0].style.webkitTransitionProperty = "all";
					currPage[0].style.webkitTransitionDuration = ".5s";
					currPage[0].style.webkitTransitionTiming = "ease-out";
	
					prevPage[0].style.webkitTransitionProperty = "all";
					prevPage[0].style.webkitTransitionDuration = ".5s";
					prevPage[0].style.webkitTransitionTiming = "ease-out";

		
					if(lastY>currentY) {
						currPage[0].style.webkitTransform = matrixCurr.translate(0, 0)
						prevPage[0].style.webkitTransform = matrixPrev.translate(0, $docHeight)
					} else {
						currPage[0].style.webkitTransform = matrixCurr.translate(0, 0)
						prevPage[0].style.webkitTransform = matrixPrev.translate(0, -$docHeight)
					}
					
				},1)
			} else {		
				if(currentY > lastY) {
					prevPage.animate({left: "0%", top:"-100%"}, 500, "easeOutQuad");
					currPage.css({top:"100%"})
				} else {
					prevPage.animate({left: "0%", top:"100%"}, 500, "easeOutQuad");
					currPage.css({top:"-100%"})
				}
				
				currPage.animate({left: "0%", top:"0%"}, 450, "easeOutQuad", subpageChangeDone);
			}
			
		} else {
			isSliding=false;
		}
	} else if(!isSliding && mode == "vcard") {
		if(dir == "left") {
			peopleCards.nextVcard("right")
		} else if(dir == "right") {
			peopleCards.nextVcard("left")
		} else {
			isSliding = false;	
		}
	}
}

/*
TOP MENU PANEL CHANGE HAS COMPLETED
*/
function menuChangeDone() 
{
	if(isWebkitComp) {
		var currPage = jqPagesArr[pages[currentX][0].id] 
		var prevPage = jqPagesArr[pages[lastX][lastY].id] 

		currPage[0].removeEventListener( 'webkitTransitionEnd', menuChangeDone , false ); 		
		currPage[0].style.webkitTransitionProperty = "";
		currPage[0].style.webkitTransitionDuration = "0";
		prevPage[0].style.webkitTransitionProperty = "";
		prevPage[0].style.webkitTransitionDuration = "0";
	}
	
	if(currentX == 0) {
		if(miniGame)
		jqSpaceship.fadeIn()	
	}
	
	lastX=currentX;
	lastY=currentY;
		
	var currentID = pages[currentX][currentY].id

	for(var xx in jqPagesArr) {
		if(currentID != xx) {
			jqPagesArr[xx].css({display: "none"})
			jqPagesArr[xx].removeAttr("style")				
			jqPagesArr[xx].css({webkitAnimationName: ""})
		}
	}
	
	//check if subpages exists and then draw the submenu
	if(pages[currentX].length>1){	

		jqLeftmenu.empty()		
		jqLeftmenu.append(leftMenuArr[pages[currentX][0].id])	

		var newPos = ($docHeight*.5) - (jqLeftmenu.height()*.5) 
		jqLeftmenu.css({top:newPos + "px"})		
		
		if(isWebkitComp) {
			removeSubMenuItems()
			jqLeftmenu.css({display:"table"})
			jqLeftmenu[0].style.webkitTransitionDuration = ".3s";
			setTimeout(function(){
				var tranf = window.getComputedStyle(jqLeftmenu[0]).webkitTransform
				
				if(tranf != "none") {
					var leftMatrix = new WebKitCSSMatrix(tranf)
					//jqLeftmenu[0].style.webkitTransform = leftMatrix.translate(leftMatrix.e - leftMatrix.e + 100)		
					jqLeftmenu[0].style.webkitTransform = leftMatrix.translate( - leftMatrix.e)
				} else {
					var leftMatrix = new WebKitCSSMatrix()
					//jqLeftmenu[0].style.webkitTransform = leftMatrix.translate(leftMatrix.e - leftMatrix.e + 100)		
					jqLeftmenu[0].style.webkitTransform = leftMatrix.translate( - leftMatrix.e)
				}
			},10);
		} else {
			jqLeftmenu.css({right:"-100px", display:"table"})
			jqLeftmenu.animate({right:"0%"}, 300, "easeOutQuad", function() {});	
		}
		markLeftMenu(0)
	}

	isSliding=false;
	
}

/*
SUB PAGE HAS BEEN CHANGED
*/

function subpageChangeDone()
{
	if(isSliding) {

		if(isWebkitComp) {
			jqPagesArr[pages[currentX][currentY].id][0].removeEventListener( 'webkitTransitionEnd', subpageChangeDone , false ); 			
			jqPagesArr[pages[currentX][currentY].id][0].style.webkitTransitionProperty = "";
			jqPagesArr[pages[currentX][currentY].id][0].style.webkitTransitionDuration = "0";			
			jqPagesArr[pages[currentX][lastY].id][0].style.webkitTransitionProperty = "";
			jqPagesArr[pages[currentX][lastY].id][0].style.webkitTransitionDuration = "0";
		}
		
		lastX=currentX;
		lastY=currentY;

		var imgsrc = ""

		//Load background image if exists	
		var imgcontainer = jqPagesArr[pages[currentX][currentY].id].find(".bgimgcontainer")
		
		if(imgcontainer.children().length == 0) {

			if(isWebkitComp || cssBackground == true) {
				
				for(var i=1; i< pages[currentX].length; i++) {				
					if(i == currentY ) {
						imgsrc = jqPagesArr[pages[currentX][currentY].id].find(".bgimgcontainer").attr("img")
					} else if(i == currentY-1 ) {
						imgsrc = jqPagesArr[pages[currentX][currentY-1].id].find(".bgimgcontainer").attr("img")
					} else if(i == currentY+1 ) {
						imgsrc = jqPagesArr[pages[currentX][currentY+1].id].find(".bgimgcontainer").attr("img")
					} else {
						imgsrc = ""
					}

					var contentDiv = jqPagesArr[pages[currentX][i].id].find(".content")
					
					if(imgsrc == "") {
							contentDiv.css({"background-image":"none"});
					} else {
						contentDiv.css({
							"background-image":"url(/" + imgsrc + ")",
							"background-repeat": "no-repeat",	
							"background-position":"center center",
							"-webkit-background-size":"100% 100%",
							"-webkit-background-clip":"padding-box"
						})
					}
				}
				
				var docHeight = $docHeight
				var docWidth = $docWidth
				
				if((typeof Touch == "object")) {					
					if(orientation == 0 || orientation == 180) {
						docHeight = 1024
						docWidth = 768
					} else {
						docHeight = 768
						docWidth = 1024
					}					
				}
				
				var scaleFactor =  Math.max(docHeight/576, docWidth/1024)
				$(".content").css({
					"-webkit-background-size": "" + (1024*scaleFactor) + "px" + " " + (576*scaleFactor)+"px",
				})
				
			} else {
				
				var bgimg = new Image()
				bgimg.width = 1024
				bgimg.height = 576
				var imgSrc = "/" + imgcontainer.attr("img")
				
				if(imgSrc != "") {
					$(bgimg).load(function(){
						//$(this).hide()			
						imgcontainer.append(this)
						//$(this).fadeIn()	
						resposElements()						
					}).attr('src', imgSrc);
				}
			}			
		}		
		isSliding=false;
	}
}

/*
* CLEAR SUB MENU
*/
function removeSubMenuItems() {	
	if(isWebkitComp) {
		var tranf = window.getComputedStyle(jqLeftmenu[0]).webkitTransform
		
		if(tranf != "none") {
			var leftMatrix = new WebKitCSSMatrix(tranf)
	
			//$("#leftMenu")[0].style.webkitTransitionDuration = "0s";		
			//$('#leftMenu').css({display:"none"})
			
			setTimeout(function(){
				jqLeftmenu[0].addEventListener( 'webkitTransitionEnd', submenuRemoved , false ); 
				//jqLeftmenu[0].style.webkitTransform = leftMatrix.translate( - leftMatrix.e)
				jqLeftmenu[0].style.webkitTransform = leftMatrix.translate(leftMatrix.e - leftMatrix.e + 100)	
			}, 10);
		}

	}else {
		jqLeftmenu.animate({right:"-100px"}, 300, "easeOutQuad", function() { jqLeftmenu.empty();jqLeftmenu.css({display:"none"}) });		
	}
}
function submenuRemoved() {
	jqLeftmenu[0].removeEventListener( 'webkitTransitionEnd', submenuRemoved , false ); 
	
}

/*
MAKE SURE APPLE DEVICES DO NOT USE DEFAULT SCROLLING
*/

function BlockMove(event) {
	// Tell Safari not to move the window.
	event.preventDefault();
}

/*
* OPEN SHOWCASE
*/
function showcaseOpen()
{
	_gaq.push(['_trackPageview', lang + "/showcase"]);	
	
	if(peopleCards)
	peopleCards.closePeopleCard();
	
	if(miniGame)
	miniGame.hideCanvas()

	if(isWebkitComp) {	

		if(!showCaseMatrix) {
			var tranf = window.getComputedStyle($('#showcase')[0]).webkitTransform
		
			//console.log(tranf)
			if(tranf != "none")
			showCaseMatrix = new WebKitCSSMatrix(tranf)
			else 
			showCaseMatrix = new WebKitCSSMatrix()
			
		}
		
		jqShowCase.css({display: "block", top:"0px"});		
		jqShowCase[0].style.webkitTransform = showCaseMatrix.translate(0, $docHeight)
		setTimeout(function(){	
			jqShowCase[0].style.webkitTransitionProperty = "all";
			jqShowCase[0].style.webkitTransitionDuration = ".5s";
			jqShowCase[0].style.webkitTransitionTiming = "ease-out";
			jqShowCase[0].style.webkitTransform = showCaseMatrix.translate(0,  - showCaseMatrix.f)

		},1)
	} else { 
		jqShowCase.css({display: "block", top:$docHeight + "px"});
		jqShowCase.animate({top:0}, 400, "easeOutQuad");
	}
	
	$(window).data('sitePosition', $(".page:visible"));	
	isWebkitComp && jqShowCase[0].addEventListener( 'webkitTransitionEnd', hideSite , false );
}

/*
* CLOSE SHOWCASE
*/

function showcaseClose() 
{
	$(window).data('sitePosition').show();
	
	if(isWebkitComp) {				
		jqShowCase.css({display: "block", top:"0px"});			
		jqShowCase[0].addEventListener( 'webkitTransitionEnd', showCaseCloseDone , false ); 
		jqShowCase[0].style.webkitTransform = showCaseMatrix.translate(0, $docHeight);
	} else {
		jqShowCase.animate({top:"100%"}, 600, '', showCaseCloseDone);
	}
}

function showCaseCloseDone()
{
	if(isWebkitComp) {			
		jqShowCase[0].removeEventListener( 'webkitTransitionEnd', showCaseCloseDone , false ); 
		jqShowCase[0].style.webkitTransitionProperty = "";
		jqShowCase[0].style.webkitTransitionDuration = "0";
		jqShowCase.css("display", "none")
	}
	
	if(currentX == 0) {
		if(miniGame)
		miniGame.showCanvas()
	}
		
}

function hideSite() {
	jqShowCase[0].removeEventListener( 'webkitTransitionEnd', hideSite , false );
	$(window).data('sitePosition').hide();
}

/*
GET AN ELEMENT ARRAY BASED ON CLASSNAME
*/
function getElementsByClass( searchClass, domNode, tagName) { 
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) { 
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1) 
			el[j++] = tags[i];
	} 
	return el;
} 

/*
REPOSITION ELEMENTS ON RESIZE
*/
function resposElements() {
						   
	var scaleFactor	
	$docHeight = $("body").innerHeight()
	$docWidth = $("body").innerWidth()	
	
	if(isIpadComp) {	
		/*
		setTimeout(function(){
			if(orientation != prevOrientation) {
				document.location.href = "#"
				location.reload()
			}
		},100);
		
		
		if(orientation == 0 || orientation == 180) {
			$docHeigh = 1024
			$docWidth = 768
		} else { 
			$docHeigh = 768
			$docWidth = 1024
		}
		
		scaleFactor =  Math.max($docHeigh/576, $docWidth/1024)
		$(".content").css({
			"-webkit-background-size": "" + (1024*scaleFactor) + "px" + " " + (576*scaleFactor)+"px",
		})
		*/
		
	} else {

		scaleFactor =  Math.max($docHeight/576, $docWidth/1024)
		var xpos = ($docWidth - (1024*scaleFactor)) * .5
		var ypos = ($docHeight - (576*scaleFactor)) * .5
		
		if(cssBackground == true) {
			$(".content").css({
				"-webkit-background-size": "" + (1024*scaleFactor) + "px" + " " + (576*scaleFactor)+"px",
			})	
		} else {	
			$(".bgimgcontainer").each(function(i,j) {	
				$(this).find("img").css({"webkit-backface-visibility": "hidden", position:"absolute", top:ypos + "px", left:xpos + "px", width:(1024*scaleFactor) + "px", height:(576*scaleFactor) + "px"})
				$(this).css({clip:"rect(0px," + $docWidth +"px," + $docHeight + "px, 0px)"})							   
			});
		}
	}
	
	var newPos = ($docHeight*.5) - (jqLeftmenu.height()*.5) 
	jqLeftmenu.css({top:newPos + "px"})
	
}

/*
SPECIAL LOAD FUNCTION THAT TRIGGERS EVEN IF IMAGE IS CACHED
*/
(function ($) {
$.event.special.load = {
add: function (hollaback) {
if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
// Image is already complete, fire the hollaback (fixes browser issues were cached
// images isn't triggering the load event)
if ( this.complete || this.readyState === 4 ) {
hollaback.handler.apply(this);
}

// Check if data URI images is supported, fire 'error' event if not
else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
$(this).trigger('error');
}

else {
$(this).bind('load', hollaback.handler);
}
}
}
};
}(jQuery));

// This hotfix makes older versions of jQuery UI drag-and-drop work in IE9
(function($){var a=$.ui.mouse.prototype._mouseMove;$.ui.mouse.prototype._mouseMove=function(b){if($.browser.msie&&document.documentMode>=9){b.button=1};a.apply(this,[b]);}}(jQuery));


