var popuped = false
var compact = false
var bookmarked = false
var url = window.location.href
var mark = url.indexOf("#")
if(mark != '-1'){
	var path = url.substr(mark+1)	
	bookmarked = true
}
var test = false
if( url.indexOf('test.inet') > -1 ) {
	test = true
}

/* LAYOUT functions */

//if screen resolution is <= 1024px; switch to compact layout mode
function setPage(){

	( screen.width <= 1024 ) ? compact = true : compact = false
	compact ? document.body.className = 'home compact' : document.body.className = 'home default'

}

//add a div to parent element with classname and id
function addDiv(_class, _id, _parent){
	try{
		element = document.createElement("<div class='" + _class + "' id='" + _id + "'></div>")
	} catch(e) {
		element = document.createElement("div")
		element.setAttribute("id", _id)
		element.setAttribute("class", _class)
	}
	var obj = document.getElementById(_parent)
	obj.appendChild(element) 
}

//remove div of parent by id
function removeDiv(_divId,_parent){
	var d = document.getElementById(_divId)
	var p = document.getElementById(_parent)
	p.removeChild(d)
}

// switch layout modes
function switchLayout(){
	var mode = document.body.className
	if(mode.indexOf('compact') > -1) {
		switch2default()
	} else {
		switch2compact()		
	}
}
// switch layout to compact mode
function switch2compact() {

	compact = true
	
	document.body.className = 'home compact'
	
	removeDiv('bg','main')
	removeDiv('bg_mddl','main')
	removeDiv('bg_bottom','main')	

	document.getElementById('main').style.height = ''
	
	//remove compact promobox
	removeDiv('promobox','main')

	//add default promobox
	addDiv('thumbgame','promobox','thumb')

	//change gamefinder source file
	var gf = new SWFObject("swf/gamefinder_compact.swf", "swf_thumb", "785", "484", "8", "#FFFFFF")
	gf.addParam("scale", "noscale")
	gf.addParam("wmode", "transparent")

	gf.addVariable("homeBtn", translations[0])
	gf.addVariable("switchBtn", translations[1])

	gf.addVariable("gameList", translations[4])
	gf.addVariable("playNow", translations[5])
	gf.addVariable("newText", translations[6])
	gf.addVariable("home", translations[7])
	
	gf.write("thumb_swf")

}

// switch layout to default mode
function switch2default(){

	compact = false

	document.body.className = 'home default'

	addDiv('bg','bg','main')
	addDiv('bg_mddl','bg_mddl','main')
	addDiv('bg_bttm','bg_bottom','main')	

	document.getElementById('main').style.height = ''

	//remove compact promobox
	removeDiv('promobox','thumb')

	//add default promobox
	addDiv('swf','promobox','main')
	
	var pb = new SWFObject("swf/disneygames_promobox.swf", "swf_promobox", "795", "258", "8", "#FFFFFF")
	pb.addParam("scale", "noscale")
	pb.addParam("wmode", "transparent")
	pb.write("promobox")

	//change gamefinder source file
	var gf = new SWFObject("swf/gamefinder.swf", "swf_thumb", "785", "349", "8", "#FFFFFF")
	gf.addParam("scale", "noscale");
	gf.addParam("wmode", "transparent")

	gf.addVariable("infoBtn", translations[2])	
	gf.addVariable("switchBtn", translations[3])			

	gf.addVariable("gameList", translations[4])
	gf.addVariable("playNow", translations[5])
	gf.addVariable("newText", translations[6])
	gf.addVariable("home", translations[7])
	
	gf.write("thumb_swf")

}

/* /LAYOUT functions */

function hideGame(){ 
	document.getElementById('promobox').innerHTML = '' 
	document.getElementById('promobox').style.display = 'none' 
	document.getElementById('promobox').style.visibility = 'hidden' 	
}

function changeGame(path) {

	//hide pop layer
	document.getElementById('popLayerHead').style.display='none'

	//position elements on page
	if(!compact){
		var box = document.getElementById("promobox").style
		box.display = 'none'
		box.visibility = 'hidden'
	}
	
	var length = path.length
	var ext = path.substr(length-3,3)
	var height = path.substr(length-7,3)
	var width = path.substr(length-11,3)

	if(compact && getExt(path)=='swf') {
		width = parseInt(360 * width / height)
		height = 360
	}

	if(!compact){
		var main_height = parseInt(height) + 164 //var main_height = parseInt(height) + 164
		var mddl_height = parseInt(height) + 164 - 340
		
		document.getElementById("main").style.height = main_height + "px" 	// height + 164
		document.getElementById("bg_mddl").style.height = mddl_height + "px" // height + 164 - 341


	var promo_left = parseInt(((795-parseInt(width))/2) + 11 )	
	box.left = promo_left + "px"
	compact ? box.top = "27px"	: box.top = "108px"	
	box.width = width + "px"
	box.height = height + "px"

	}
	
	//POPUP(layered) games

		//default mode popups
		if(path.indexOf('MickeyRound_13-15') > -1) {
			popuped = true
			embedPop('MickeyRound_13-15_800x600.swf')
			return
		}	
		
		if(path.indexOf('MickeyRound_10-12') > -1) {
			popuped = true
			embedPop('MickeyRound_10-12_800x600.swf')
			return
		}
			
		if(path.indexOf('MickeyRound_7-9') > -1) {
			popuped = true
			embedPop('MickeyRound_7-9_800x600.swf')
			return
		}
		
		if(path.indexOf('MickeyRound_4-6') > -1) {
			popuped = true
			embedPop('MickeyRound_4-6_800x600.swf')
			return
		}
		
		if(path.indexOf('MickeyRound_1-3') > -1) {
			popuped = true
			embedPop('MickeyRound_1-3_800x600.swf');
			return
		}
	
		if(path.indexOf('Lilo_Stitch_DanceMania') > -1) {
			popuped = true
			embedPop('Lilo_Stitch_Lilo_DanceMania_779x509.dcr')
			return
		}
		
		//in compact mode, all games are popedup
		if(compact){
			popuped = true
			embedPop(path)
			return
		}
		



	//COOKIES
	cookied = readCookie('wdgms')
	if(cookied){

		//embed games
		getExt(path) == 'swf' ? embedSwf(path) : embedDcr(path)

	} else {
		
		createPrerollCookie('wdgms')

		if(compact && getExt(path)=='swf') {
			width = parseInt(360 * width / height)
			height = 360
		}

		//embed games with preroll
		var rndm = Math.floor(Math.random()*789)
		var game = new SWFObject("swf/container.swf?" + rndm , "game", width, height , "8", "#FFFFFF")
//		game.addParam("wmode", "transparent")
		//preroll var: game to load after
		game.addVariable("gid", path )

		game.write("promobox")

	}

	box.display = 'block'
	box.visibility = 'visible'	
	box.position = 'absolute'

	compact ? document.body.className = 'gamesite compact' : document.body.className = 'gamesite default'
	
	// hitbox (statistics)
	if(!bookmarked) hitDaBox(path)
	
}
function embedPop(path){

	//for ex. path='Lilo_Stitch_Lilo_DanceMania_779x509.dcr'

	self.scrollTo(0,106)

	var length = path.length
	var height = path.substr(length-7,3)
	var width = path.substr(length-11,3)	
	var jpg = path.substr(0,length-12)	

	if(compact) {

		width = parseInt(360 * width / height)
		height = 360

		if(path.indexOf('chicken_little_load_550x470.swf') > -1) {
			width = 550
			height = 470
		}
		
	}

	document.getElementById('popLayerHead').style.display='block'
	document.getElementById('popLayerHead').style.width = document.getElementById('popLayerBox').style.width = width + 'px'
	var promo = document.getElementById('promobox')

	if(!compact){
		promo.innerHTML = "<img src='i/pop_" + jpg + ".jpg'>" 
		//for ex. path='pop_Lilo_Stitch_Lilo_DanceMania.jpg'
		//this image is displayed below the game layer
	
		promo.style.display = 'block'
		promo.style.visibility = 'visible'
	} 
	
	cookied = readCookie('wdgms')
	if(cookied){
		
		//embed game
		getExt(path) == 'swf' ? embedSwf(path) : embedDcr(path)
		
	} else {

		createPrerollCookie('wdgms')

		//embed game with PREROLL
		var rndm = Math.floor(Math.random()*789)
		var pgame = new SWFObject("swf/container.swf?" + rndm , "pgame", width, height , "8", "#FFFFFF")
		//preroll var
		pgame.addVariable("gid", path )
		pgame.write("popLayerContent")
		
	}
	
	//show layer
	document.getElementById('popLayer').style.display = 'block'
	
		// hitbox (statistics)
    if(!bookmarked) hitDaBox(path)
	
}

function changeGameFromTop5(path) {
	self.scrollTo(0,106)
	changeGame(path)
}

function getExt(path){

	var length = path.length
	var ext = path.substr(length-3,3)
	return(ext)
	
}

function embedSwfFromPreroll(path){
	embedSwf(path)
}
function embedSwf(path){

	//flash games
	var length = path.length
	var height = path.substr(length-7,3)
	var width = path.substr(length-11,3)

	var bgcolor = "#FFFFFF"
	
	//incred. memo
	if(path.indexOf('Incredibles_Memory_game') > -1) bgcolor = "#000000"

	var rndm = Math.floor(Math.random()*789)

	if(compact && getExt(path)=='swf') {
		width = parseInt(360 * width / height)
		height = 360
		
		if(path.indexOf('chicken_little_load_550x470.swf') > -1) {
			width = 550
			height = 470
		}
	}


	var game = new SWFObject("games/" + path + "?" + rndm , "game", width, height , "8", bgcolor)
//	game.addParam("wmode", "transparent")
	game.addParam("scale", "noborder")

	//preroll var
	//game.addVariable("gid", path )
	
		//tunetown
		if(path.indexOf('tunetown') > -1){  
			game.addVariable("localPath", "games/")
			game.addVariable("maxBg", "7")
			game.addVariable("maxToon", "2")			
			game.addVariable("maxTune", "7")
			game.addVariable("path", "http://www.disney.co.uk/toontown/tunetown.html")
			game.addVariable("ID", "syn")	
			game.addVariable("Media", "TuneTownPage2Email")
		}

	if(!popuped) {
		game.write("promobox")
	} else  {
		game.write("popLayerContent")
		popuped = false
	}

	
}

function embedDcr(path){
		
	//director games
	var length = path.length
	var height = path.substr(length-7,3)
	var width = path.substr(length-11,3)

	if(compact) {
		width = parseInt(360 * width / height)
		height = 360
	}

	if(compact) {var bgcolor = '#587dbd'} else { var bgcolor = '#ffffff' }

	var dcr_embed = '<object classid="clsid:166B1BCA-3F9C-11CF-8075-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=10,0,0,0" width="' + width + '" height="' + height + '"><param name="src" value="games/' + path + '"><PARAM NAME=bgColor VALUE=' + bgcolor + '><PARAM NAME=swStretchStyle VALUE="meet"><PARAM NAME=swstretchVAlign VALUE="center"><embed src="games/' + path + '" pluginspage="http://www.macromedia.com/shockwave/download/" width="' + width + '" height="' + height + '" bgColor= ' + bgcolor + ' swStretchStyle="meet" swstretchVAlign="center" ></embed></object>'
//http://publib.boulder.ibm.com/infocenter/rtnlhelp/v6r0m0/index.jsp?topic=/com.ibm.etools.webreference.doc/topics/rjsfallmedia.html
	if(!popuped) {
		
		var dcrGame = document.getElementById("promobox")	
		dcrGame.innerHTML = dcr_embed
		//reinsert object
		dcrGame.outerHTML = '<div class="swf" id="promobox">' + dcr_embed + '</div>'
	
		positionPromoBox(height, width)
		
	} else {
		
		var dcrGame = document.getElementById("popLayerContent")	
		dcrGame.innerHTML = dcr_embed
		//reinsert object
		dcrGame.outerHTML = '<div id="popLayerContent">' + dcr_embed + '</div>'
		pouped = false

	}

}

function positionPromoBox(height, width) {

	var box = document.getElementById("promobox").style
	var promo_left = parseInt(((795-parseInt(width))/2) + 11)

	//box.top = "58px" //box.top = "108px"	
	compact ? box.top = "27px" : "108px"

	box.left = promo_left + "px"
	box.width = width + "px"
	box.height = height + "px"
	box.display = 'block'
	box.visibility = 'visible'	
	box.position = 'absolute'

}

function hitDaBox(game) {
	if( !test ) {
		_hbflash(game,_mlc,'n','n')
	} 
}

function checkBookmark(){

	var url = window.location.href
	var mark = url.indexOf("#")

	if(mark != '-1'){
		var path = url.substr(mark+1)	
		//document.location.href = url.substr(0,mark+1)
		changeGame(path)
	}
}

function showHelp( _switch ){

	self.scrollTo(0,106)

	var help = new SWFObject("swf/info.swf" , "info", "710", "580" , "8", "#000000")
	help.write("help")
	
	document.getElementById('helpLayer').style.display='block'
	
}

function closeFlashLayer(){
	
	document.getElementById('helpLayer').style.display='none'
	document.getElementById('help').innerHTML = ''
	
}

/* cookie functions */

function createPrerollCookie(path) {

        var cookieName = escape(path)
		createCookie(cookieName,"true",false)
		// no days parameter to make a session cookie
        return
			
}
function createCookie(name,value,days) {
	
	if (days) {
		var date = new Date()
		date.setTime(date.getTime()+(days*24*60*60*1000))
		var expires = "; expires="+date.toGMTString()
	}
	else
	var expires = ""
	document.cookie = name+"="+value+"; path=/"
	
}

function readCookie(name) {
	
	var nameEQ = name + "="
	var ca = document.cookie.split(';')
	for(var i=0;i < ca.length;i++) {
		var c = ca[i]
		while (c.charAt(0)==' ') c = c.substring(1,c.length)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length)
	}
	return null
	
}

function eraseCookie(name) {
	createCookie(name,"",-1)
}

/* /cookie functions */

function getSponsor(path){
/*
//get sponsors id
		 jQuery.ajax({
			 type: "GET",
			 url: "xml/gamefinder.xml",
			 dataType: "xml",
			 success: function(xml) {
				 jQuery(xml).find('item').each(function(){
					if( jQuery(this).attr('link') == path ) {
						sponsorID = jQuery(this).attr('sponsor')



					}
				 }); 
			 }
			 
		 }); 
	
//get sponsor datas
		 jQuery.ajax({
			 type: "GET",
			 url: "xml/sponsor.xml",
			 dataType: "xml",
			 success: function(xml) {
			 
				var redirect = jQuery(xml).find('redirect').text() 
				//'http://disney.co.uk/DisneyOnline/j/redirect.jsp?redirectURL='
				
				 jQuery(xml).find('item').each(function(){
					if( jQuery(this).attr('id') == sponsorID ) {
	
						var sponsorPic = jQuery(this).find('picture').text()
						var sponsorLink = jQuery(this).find('link').text()	

				//embed it
				
						var sp = new SWFObject("swf/sponsor.swf", "swf_sponsor", "160", "70", "8", "#FFFFFF");
						sp.addParam("scale", "noscale");
						sp.addParam("wmode", "transparent");
						sp.addVariable("sponsorTxt","sponsored by:")
						sp.addVariable("SponsorImage",sponsorPic)
						sp.addVariable("SponsorLink",redirect + sponsorLink)
						sp.write("sponsor");							 
							 
							 
					}
				 });
			 }
		 }); 
*/
}

// tunetown miatti vaktolteny
function getHash(){}
function getVars(){}
// incredibles top trumps miatt
function banload(){}

function bookmarkHitboxCheck(){
	var url = window.location.href
	var mark = url.indexOf("#")
	if(mark != '-1'){
		var path = url.substr(mark+1)
		hitDaBox(path)
		bookmarked = false
	}	
}

//checks if its a direct link to a game
window.onload = bookmarkHitboxCheck

