/* -----------------------  Constants -------------------- */
var debug = true;
var pageInited = false;

// ------------------  Enviroment Variables -----------------
// get page width and height
var pageWidth;
var pageHeight;

function updatePageSize(){
	if (self.innerHeight) // all except Explorer
	{
		pageWidth = self.innerWidth;
		pageHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		pageWidth = document.documentElement.clientWidth;
		pageHeight = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		pageWidth = document.body.clientWidth;
		pageHeight = document.body.clientHeight;
	}
}




// Register Event handler	
// get active page url
var activeUrl = location.href;
// get if it's a local test
var localTest = (location.protocol=='file:');
// get document root folder
var documentRoot;
if(localTest){
	documentRoot = activeUrl.substring(0,activeUrl.lastIndexOf('/html/')+6);
}else{
	/* TODO: if testing on server enviroment, where document root is in subfolder,
	then define exceptions for particular domain */ 
	if(activeUrl.lastIndexOf('/fruitopia/')>0){
		documentRoot = location.hostname+'/fruitopia/';
	}else{
		documentRoot = location.hostname+'/';
	}
	documentRoot = location.protocol+"//"+documentRoot;
}

log("documentRoot:"+documentRoot);



function initSubPage(){	
	
	log('initSubPage');	
	//log((localTest)?'the page is being tested locally':'the page is on webserver');	
	/* ----------- Flash Navigation ---------------*/
	if(!pageInited && $('navigation')){
		// create flash navigation container
		var flashNavigationTarget = document.createElement('div');
		flashNavigationTarget.id = "flashNavigationTarget";
		$('metanavigation').appendChild(flashNavigationTarget);
		
		var parameters = new Array();
		var html_parameters = window.location.search.substr(1).split("&");
		for (var i=0; i<html_parameters.length; i++) {
			parameter = html_parameters[i].split("=");
			parameters[parameter[0]] = parameter[1];
		}
		
		// rewrite navigation with flash version
		var naviObject = new SWFObject(documentRoot+'swf/navigation.swf', 'flashNavigation', '850', '120', '8');
		naviObject.addParam('wmode','transparent'); 
		naviObject.addParam('allowScriptAccess','always'); 
		naviObject.addVariable('activeUrl', activeUrl); // send active page url 
		naviObject.addVariable('documentRoot', documentRoot); // send the root directory url
		naviObject.addVariable('stopAnimation', 'false'); //to jump notover fade in of navigation 
		naviObject.addVariable('soundCookie', getCookie("sound")); //checks cookie -> sound is on or off
		if (parameters["startpage"])  naviObject.addVariable('startpage', parameters["startpage"]);
		naviObject.addParam("menu","false"); 
		naviObject.addParam("wmode","transparent"); 
		naviObject.write($('navigation'));
		
		// move html navigation out of sight, print styleshhet brings it back
		var htmlNavigation = $$('div#navigation ul');
		htmlNavigation.each(function(item){
			item.addClassName('aural');
		})		
		
		// init metanavigation popups
		var openMeta = function (item){
			pop(item.href);
			return false;
			
		}
		
		var metaNavigation = $('metanavigation');
		metaNavigation.innerHTML = "";
		/*$$('#metanavigation a').each(function(item){
			if(item.title == 'Kontakt'){
				item.onclick = function (item){
					pop(this.href,320,400);
					return false;				
				}
			}else if(item.title == 'Rechtliche Hinweise'){
				item.onclick = function (item){
					pop(this.href,545,600,'no','yes');
					return false;				
				}
			}

				
		})*/		
		
		// init product list if needed
		if($$('.productList').length > 0){
			initProductPage();
		}
		
	}
	
	// init registration page
	if(!pageInited && $('flashRegistrationContent')){
		initRegistrationPage();
	}
	
}

function initRegistrationPage() {
	
	var parameters = new Array();
	var html_parameters = window.location.search.substr(1).split("&");
	for (var i=0; i<html_parameters.length; i++) {
		parameter = html_parameters[i].split("=");
		parameters[parameter[0]] = parameter[1];
	}
	
	var registrationObject = new SWFObject(documentRoot+'swf/registration.swf', 'flashRegistration', '730', '700', '8');
	//sregistrationObject.addParam('wmode','transparent'); 
	registrationObject.addParam('allowScriptAccess','always'); 
	registrationObject.addVariable('activeUrl', activeUrl); // send active page url 
	registrationObject.addVariable('documentRoot', documentRoot); // send the root directory url
	registrationObject.addParam("menu","false"); 
	if (parameters["startpage"])  registrationObject.addVariable('startpage', parameters["startpage"]);
	if (parameters["optin_id"])  registrationObject.addVariable('optin_id', parameters["optin_id"]);
	if (parameters["optin_hash"])  registrationObject.addVariable('optin_hash', parameters["optin_hash"]);
	registrationObject.write($('flashRegistrationContent'));
	
	$('flashRegistration').focus();

}

function openContact() {
	pop("http://www.coca-cola-gmbh.de/note.htm?fruitopia",320,400);
}

function initProductPage(){
	
	var productMcs = new Array();
	productMcs.push('swf/100pro_saft_orange.swf');
	productMcs.push('swf/100pro_saft_apfel.swf');
	productMcs.push('swf/100pro_saft_multi.swf');
	
	// used to get reffrence of a flash movie
	var getMovieName = function (movieName) {
	    if (navigator.appName.indexOf("Microsoft") != -1) {
	    	return window[movieName]
	    }
	    else {
	    	return document[movieName]
	    }
    }
	
	// get all product items
	var items = $$('.productList .item');	
	for(var i=0, l=items.length;i<l;i++){
		var itemContainer = items[i];
		var flashName = 'flashAnimation'+i;
		var flashObject = new SWFObject(documentRoot+productMcs[i], flashName, '142', '167', '8');
		var flashContainer = new Insertion.Top(itemContainer,'<div id="animation'+i+'" class="animation"></div>');
		flashObject.addParam("allowScriptAccess","sameDomain");
		flashObject.addParam("wmode","transparent"); 
		flashObject.addParam("menu","false"); 
		flashObject.write($('animation'+i));
		
		itemContainer.addClassName('flashInside');		
		// define link mouse over actions		
		var links = itemContainer.getElementsBySelector('li a');
		links.each(function(s,index){
			s.rel = flashName;
			s.onmouseover = function(){
		 		getMovieName(this.rel).showBottle(index); 
			}
			s.onmouseout = function(){
		 		getMovieName(this.rel).showBottle(); 
			}			
		})		
		
	}	
	
}

function initHomePage(){
	log('initHomePage');	
	
	// homepage schould have no scrollbars


	// remove html background
	$$('div.wrapper')[0].setStyle({
	  		backgroundImage: 'none',
			backgroundColor: 'transparent'
	});	
	
	if(!pageInited){
		
		// create container for the flash movie
		var flashHomepageContainer = document.createElement('div');
		flashHomepageContainer.id = "flashHomepageContainer";
		var wrapper = $$('.wrapper')[0];
		wrapper.appendChild(flashHomepageContainer);
		
		var homepageObject = new SWFObject('swf/homepage_loader.swf', 'flashHomepage', '100%', '100%', '8', "#ffffff");
		homepageObject.useExpressInstall('swf/expressinstall.swf');
		homepageObject.addParam('allowScriptAccess','always'); 
		homepageObject.setAttribute('redirectUrl', 'http://www.minute-maid.de/noflash.html'); // must be the absolute URL to your site 
		homepageObject.addVariable('activeUrl', activeUrl);  // send active page url 
		homepageObject.addVariable('documentRoot', documentRoot); // send the root directory url
		homepageObject.addVariable('soundCookie', getCookie("sound")); //checks cookie, if sound is on or off
		homepageObject.addParam("wmode","transparent"); 
		homepageObject.addParam("menu","false"); 
		homepageObject.write($$('#home body')[0]);
	}	
}

function init(){
	if($('home')){
		log("this is homepage");
		initHomePage();
	}else{
		setSoundImage();
		initSubPage();		
	}	
	pageInited = true;

}


	

// basic popup window function
function pop(url,width,height,resize,scroll){
	width = (width)?width:320;
	height = (height)?height:400;	
	resize = (resize)?resize:"no";
	scroll = (scroll)?"yes":"no";
	var myPopUp = window.open(url,'fruitopiaPopup','width='+width+',height='+height+',toolbar=no,location=yes,directories=no,status=no,menubar=no,scrollbars='+scroll+',resizable='+resize);
}


function log(msg){
	if(debug){
		try{
			//console.debug(msg);
		}catch(err){
			//ignore if firebug is not installed
		}	
	}
}

/* Adapt layout to resolution */

/*  add a style fix for high resolution */


function onResize(){
	updatePageSize();
	log("pageHeight:"+pageHeight);
	if(pageHeight>650){	// high resolution layout
		$$('html')[0].addClassName('highRes');

		
	}
}

//switch the sound to on or off
function switchSound(){
	//alert(getCookie("sound"));
	if(getCookie("sound")==0){
		setCookie("sound",1);		
	}
	else{
		setCookie("sound",0);		
	}  
	setSoundImage();
}

//looks for the sound state and adds the right soundimage
function setSoundImage(){

 var img= new Image(); 
 if(getCookie("sound")==0){
 	img.src = documentRoot+"img/icon_nosound.gif";
 } else{
 	img.src = documentRoot+"img/icon_sound.gif";
 }
 var newImage = document.getElementById("sound_image");
 newImage.src = img.src;
 return documentRoot+"img/icon_nosound.gif";
}

//setting cookie and rewrites the flashnavigation the really stop the sound
function setCookie(name, value) {
	  expires=new Date(new Date().getTime() + (356*24*60*1000));
    document.cookie= name + "=" + escape(value) + "; expires="+expires.toGMTString()+"; path=/;";
  	
  	// rewrite navigation with flash version to active or inactivate sound
		var naviObject = new SWFObject(documentRoot+'swf/navigation.swf', 'flashNavigation', '850', '120', '8');
		naviObject.addParam('wmode','transparent'); 
		naviObject.addVariable('activeUrl', activeUrl); // send active page url 
		naviObject.addVariable('documentRoot', documentRoot); // send the root directory url
		naviObject.addVariable('stopAnimation', 'true'); //jump over fade in of navigation
		naviObject.addVariable('soundCookie', value); //checks cookie, if sound is on or off
		naviObject.addParam("menu","false"); 
		naviObject.addParam("wmode","transparent"); 
		naviObject.write($('navigation'));
}
//reading cookie
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


onResize();

	
Event.observe(window, 'load', init, false);
Event.observe(window, 'resize', onResize, false);
