// JavaScript Document

var img_array = new Array();
var image_id = 'webbanner_main';
var swap_id = 'webbanner_swap';
var descr_id = 'webbanner_descr';
var bannerIndex = 0;
var banner_change;
var descr_change;


YUI.namespace('nsAnimations');

YUI().use('anim','node','event', function (Y) {
	var description_text = '';
	var posX = 0;
	var posY = -350;
	var direction = 'l'; // b... Bottom(default) || t... Top || l... Left || r... Right
	var siteid = 0;
	var lang = "cr";
	var color = '#ffffff';
	var font_size = '26';
	var index = 0;
	
	var fader = new Y.Anim({  
		node: '#webbanner_swap',  
		from: {
			opacity: 1
		},
		to: {  
			opacity: 0
		},  
		duration: 1  
	});  
		
	var fadeout_descr = new Y.Anim({  
		node: '#webbanner_descr',  
		from: {
			opacity: 1
		},
		to: {  
			opacity: 0,
			top: -75
		},  
		duration: 0.5,
		easing: Y.Easing.backIn
	});
	
	var fadein_descr = new Y.Anim({  
		node: '#webbanner_descr',  
		from: {
			opacity: 0,
			top: -350,
			left: '0px'
		},
		to: {  
			opacity: 1,
			top: -330,
			left: '0px'
		},  
		duration: 0.5,
		easing: Y.Easing.backOut
	});
	
	var move_descr = new Y.Anim({  
		node: '#webbanner_descr',  
		to: {  
			top: -320
		},  
		duration: 4  
	});
	
	// PUBLIC FUNCTIONS
	YUI.nsAnimations.FadeIn = function() {
		fadein_descr.run()
	};
	
	YUI.nsAnimations.Fade = function(Index,PosX,PosY,Direction,text,Siteid,Lang,Color,Font_size) {
		fader.run();
		fadeout_descr.run();
		posX = PosX;
		posY = PosY;
		direction = Direction;
		description_text = text;
		siteid = Siteid;
		if(Lang!="") {
			lang = Lang;
		}
		color = Color;
		font_size = Font_size;
		document.getElementById('webbanner_item'+index).style.borderColor = '#000000';
		index = Index;
	}
	
	function showDescription() {
		//document.getElementById('webbanner_descr').style.height = (posY*(-1)) + 'px';
		updateDirection();
		document.getElementById('webbanner_descr').innerHTML = description_text;
		document.getElementById('webbanner_arrow').style.color = color;
		document.getElementById('webbanner_descr').style.color = color;
		document.getElementById('webbanner_descr').style.fontSize = font_size + 'px';
		document.getElementById('webbanner_item'+index).style.borderColor = '#ffffff';
		if(siteid == '' || siteid == '#') {
			document.getElementById('webbanner_link').href = ("#");
		} else {
			document.getElementById('webbanner_link').href = "index.php?lan=" + lang  + "&id=" + siteid.toString();
		}
		fadein_descr.run();
	}
	
	function updateDirection() {
		//alert('Update: ' + posX + ' ' + posY);
		if(direction == "b") {
			fadein_descr.set('from',{top: -75,left: (posX+'px'),opacity: 0});
			fadein_descr.set('to', {top:posY,left: (posX+'px'),opacity: 1});
			fadeout_descr.set('to', {top: -75,left: (posX+'px'),opacity: 0});
		} else if(direction == "l") {
			fadein_descr.set('from',{top:posY,left:0,opacity: 0});
			fadein_descr.set('to', {top:posY,left: (posX+'px'),opacity: 1});
			fadeout_descr.set('to', {top:posY,left:0,opacity: 0});
		} else if(direction == "r") {
			fadein_descr.set('from',{top:posY,left: (210+'px'),opacity: 0});
			fadein_descr.set('to', {top:posY,left: (posX+'px'),opacity: 1});
			fadeout_descr.set('to', {top:posY,left: (210+'px'),opacity: 0});
		} else if(direction == "t"){
			fadein_descr.set('from',{top: -350,left: (posX+'px'),opacity: 0});
			fadein_descr.set('to', {top:posY,left: (posX+'px'),opacity: 1});
			fadeout_descr.set('to', {top: -350,left: (posX+'px'),opacity: 0});
		}
	}
	
	fadeout_descr.on('end',showDescription);
});

function init() {
	this.img_array = arguments;
	startFader();
}

function startFader() {
	if(this.banner_change == null) {
		this.banner_change = setInterval("swapBannerAuto()",4000);
	}
}

function stopFader() {
	clearInterval(this.banner_change);
	this.banner_change = null;
}

// Switch to next Banner
function swapBannerAuto() {
	document.getElementById(swap_id).style.backgroundImage = document.getElementById(image_id).style.backgroundImage;
	document.getElementById(swap_id).style.opacity = '1';
	document.getElementById(swap_id).style.filter = 'alpha(opacity = 100)';
	
	bannerIndex++;
	if(bannerIndex >= img_array.length) {
		bannerIndex = 0;
	}
	
	var elem_array = img_array[bannerIndex].split('|');
	document.getElementById(image_id).style.backgroundImage = 'url(' + elem_array[0] +  ')';
	YUI.nsAnimations.Fade(parseInt(elem_array[9]),parseInt(elem_array[4]),parseInt(elem_array[5]),elem_array[6],elem_array[1],elem_array[2],elem_array[3],elem_array[7],elem_array[8]);
}

// Switch to Banner with given Index manually. Stops autoswitching.
function swapBannerManual(index) {
	if(index == bannerIndex) {
		return;	
	}
	
	stopFader();
	
	document.getElementById(swap_id).style.opacity = '1';
	document.getElementById(swap_id).style.filter = 'alpha(opacity = 100)';
	document.getElementById(swap_id).style.backgroundImage = document.getElementById(image_id).style.backgroundImage;
	
	bannerIndex = index;
	if(bannerIndex >= img_array.length) {
		bannerIndex = 0;
	}
	
	var elem_array = img_array[bannerIndex].split('|');
	document.getElementById(image_id).style.backgroundImage = 'url(' + elem_array[0] +  ')';
	YUI.nsAnimations.Fade(parseInt(elem_array[9]),parseInt(elem_array[4]),parseInt(elem_array[5]),elem_array[6],elem_array[1],elem_array[2],elem_array[3],elem_array[7],elem_array[8]);
}

function fadeInMenu(elemId) {
	
}

// -----------------

var height;
var timer;
var isClicked = false;
	
function clearPatternBox() {
	if(isClicked == false) {
		document.getElementById('pattern').value='';
		isClicked = true;
	}
}

function MoveFooter() {
	document.getElementById('footer').style.bottom = '-50px';
	height = -50;
	timer = setInterval("FooterMovement()",10);
}

function FooterMovement() {
	document.getElementById('footer').style.bottom = height + 'px';
	height++;
	if(height > 0) {
		clearTimeout(timer);
	}
}

var snowArray = new Array();
var snowHeightArray = new Array();
var snowGeneratorTimer;
var snowMovementTimer;

function GenerateFlake() {
	if(snowArray.length >= 30) {
		clearTimeout(snowGeneratorTimer);
	}
	var snowflake = new Image();
	var rdm = (Math.floor(Math.random()*6)+1);
	
	if(rdm > 3) {
		rdm = 1;
	}
	
	snowflake.src = "cms/content/image/HP_Files/snowflake" + rdm + ".png";
	snowflake.id = rdm;
	snowflake.style.position = 'absolute';
	snowflake.style.top = '-10px';
	
	var windowWidth = 0;
	if (navigator.appName.indexOf("Microsoft")!=-1) {
 		windowWidth = document.body.offsetWidth;
	} else {
		windowWidth = window.innerWidth;
	}
	
	snowflake.style.right = Math.floor(Math.random()*windowWidth) + 'px';
	
	document.getElementById('snow').appendChild(snowflake);
	
	snowHeightArray.push(-10);
	snowArray.push(snowflake);
}

function MoveFlakes() {
	for(var i=0; i<snowArray.length; i++) {
		if(snowArray[i].id == "1") {
			snowHeightArray[i] = parseInt(snowHeightArray[i]) + 1;
		} else if(snowArray[i].id == "2") {
			snowHeightArray[i] = parseInt(snowHeightArray[i]) + 2;
		} else if(snowArray[i].id == "3") {
			snowHeightArray[i] = parseInt(snowHeightArray[i]) + 3;
		}
		
		snowArray[i].style.top = parseInt(snowHeightArray[i]) + 'px';
		
		if(parseInt(snowHeightArray[i]) >= 200) {
			snowArray[i].style.top = '-10px';
			snowHeightArray[i] = -10;
			
			var windowWidth = 0;
			if (navigator.appName.indexOf("Microsoft")!=-1) {
				windowWidth = document.body.offsetWidth;
			} else {
				windowWidth = window.innerWidth;
			}
			
			snowArray[i].style.right = Math.floor(Math.random()*windowWidth) + 'px';
		}
	}
}

function StartSnow() {
	var browserName=navigator.appName;
	if (browserName=="Microsoft Internet Explorer")
	{
	/*@cc_on 
	if (@_jscript_version < 5.7)
	{
		return;
	}
	@*/
	}

	snowGeneratorTimer = setInterval("GenerateFlake()",500);
	snowMovementTimer = setInterval("MoveFlakes()",(1000/30));
}

function StopSnow() {
	clearTimeout(snowMovementTimer);
}
