var IE = /*@cc_on!@*/false;
var IE6 = (IE && (navigator['appVersion'].indexOf('MSIE 6') > 0)) ? true : false;
var setBgA = 0;

var cloud = { 
	positions: [ [ 750, 150, 5 ], [ 800, 150, 5 ], [ 850, 150, 5 ], [ 800, 150, 5 ] ],
	phase: 0
};

$(function() { 
 $(document).ready(function(){

	setBg($("body.main").length!=0);
	
	//$("div.clouds").moveAround();
	
	$(".menu-circle").each(function(){ $(this).menuOver(); });
	
	swfobject.embedSWF("swf/clouds.swf", "clouds", "300", "100", "9.0.0", "expressInstall.swf", {}, { wmode: "transparent" });	
	
	if ($("body.funzone").length!=0) {
		swfobject.embedSWF("swf/fanta_player.swf", "fantaplayer", "520", "350", "9.0.0", "expressInstall.swf", { video_path: "../media/video/fanta_25_hu.flv" }, { wmode: "transparent" });		
	}
	
	if ($("body.products").length!=0) {
		$("div.products ul li a").each(function(){
			$(this).click(function(){
				$("div.overlay").show();
				$("div.overlay-bg").show();				
				$("div#overlay_"+$(this).parent().attr("class")).show();
			});
		});
		
		$("div.overlay-content a.close").click(function(){
				$("div.overlay").hide();
				$("div.overlay-bg").hide();				
				$("div.overlay-content").hide();
		});
	}
	
 });
});

$.fn.menuOver = function() {
	
	$(this).hover(
		function(){
			for (i=1;i<=5;i++) {
				if (i!=$(this).attr("id").split("-")[1]) { $("div.menu-group").removeClass("menu-over-"+i); }
			}
			$("div.menu-group").addClass("menu-over-"+$(this).attr("id").split("-")[1]);
		},
		function(){
			$("div.menu-group").removeClass("menu-over-"+$(this).attr("id").split("-")[1]);
		});
};

$.fn.moveAround = function() {
	$(this).animate(
		{
			left: cloud.positions[cloud.phase][0]+'px',
			top: cloud.positions[cloud.phase][1]+'px'
		},
		cloud.positions[cloud.phase][2]*1000,
		function(){
			cloud.phase = (cloud.phase+1==cloud.positions.length) ? 0 : cloud.phase+1;
			$(this).moveAround();
		}
	);
};

var contentWidth = $("div.content").width();

function setBg(main) {
	var bgminh = 645,
		bgminw = 1000,
		bgot = 101,
		bgob = 69,
		bgol = 100,
		bg = $("div#bg"), 
		wr = (main) ? $("div.wrap") : $("div.inner-wrap"),
		cn = (main) ? $("div.bottle") : $("div.content"),
		cni = $("div.content-inner"),
		ov = $("div.overlay"),
		grw = 0,
		cgrt = 0,
		cgrp = 'fixed',
		wh = $(window).height(),
		wrh = wr.outerHeight(true),
		wrw = wr.outerWidth(true),
		ovh = false,
		iwrh = false,
		ww, bgh, bgw, ot, ol;	
			
	if (wh>bgminh) {
		if (wh>wrh) {
			ot = wh-wrh;
			bgh = wh;
			cgrt = (bgh-270+73);
			iwrh = bgh-wrh;
			ovh = wh;
		} else {
			ot = wh-bgminh-bgot;
			bgh = bgminh+(wh-bgminh);
			cgrt = (bgh-270+97);
		}
	} else {
		bgh = bgminh;
		ot = 0-bgot;
		cgrt = 573;
		cgrp = 'absolute';
		ovh = parseInt($("body").height());
	}
	
	if (main) { 
		wr.css({ top: ot+'px'  });
		bg.css({ height: bgh+'px', overflow: 'hidden' });
	} else {
		if (iwrh!==false) {
			wr.css({ height: (wrh+iwrh-parseInt(wr.css("paddingBottom")))+'px' }); 
			cni.css({ height: cni.outerHeight(false)-parseInt(cni.css("paddingBottom"))+iwrh+'px' });
		}
		if (ovh!==false) {
			ov.css({ height: ovh+'px' });
		}
	}
	
	// set width later to avoid unwanted horizontal scrollbars
	ww = $(window).width();
	
	if (ww>bgminw) {
		if (ww>wrw) {
			ol = parseInt((ww-wrw)/2);
			bgw = ww;
			grw = Math.ceil((ww-wrw)/2);
			bgpl = (bgw-1600)/2;
		} else {
			ol = parseInt((ww-bgminw)/2)-bgol;
			bgw = bgminw+(ww-bgminw);
			bgpl = ((ww-bgminw)/2)-300;
		}
	} else {
		bgw = bgminw;
		ol = 0-bgol;
		bgpl = -300;
	}
	
	bg.css({ width: bgw+'px', overflow: 'hidden' });
	wr.css({ left: ol });
	
	if (!main) {
		$("body").css({ backgroundPosition: bgpl+'px top' });
		bg.css({ height: wrh });
	}
	
	if (grw!=0) {
		if (wr.find("div.grass_left").length==0 && wr.find("div.grass_right").length==0) {
			cn.after('<div class="grass_left"></div><div class="grass_right"></div>');
		}
		$("div.grass_right").css({ width: grw+'px' });
		$("div.grass_left").css({ width: grw+'px', left: '-'+grw+'px' });
	}
	
	$("div.content-grass").css({ top: cgrt+'px', position: cgrp });
	
	// run once again to make it perfect
	if (setBgA==2) { setBgA = 0; setBg(main); } else {
	if (setBgA==1) { setBgA = 2; }
	if (setBgA==0) { setBgA = 1; setBg(main); }
	}
	window.onresize = function() { setBg(main); };
}