// JavaScript Document
var currentLink;
var top_item = 100;
var picture_nbr = 1;
var prev_pic = 2;
var step_x = 0;
var step_y = 0;
var curr_left = 0;
var curr_top = 0;

var count = 0;

var pic_height = 0;
var pic_width = 0;

function startMovePicture() {
	setTimeout("moveHeaderPicture()", 100);
}

function moveHeaderPicture() {
	var e = document.getElementById("header_pic_wrapper");
	var x = document.getElementById("banner_pic");
	if(e != null && x != null) {
		if(prev_pic != picture_nbr) {
			prev_pic = picture_nbr;
			curr_left = 0;
			curr_top = 0;
			//alert("changed");
		} else {
			pic_height = x.height;
			pic_width = x.width;
			//step_x = (parseInt(pic_width) + parseInt(e.style.left ? e.style.left : 0) - 680)/20; //steps_to_go;
			//step_y = (parseInt(pic_height) + parseInt(e.style.top ? e.style.top : 0) - 146)/20; //steps_to_go;
			step_x = (parseInt(pic_width) + parseInt(e.style.left ? e.style.left : 0) - 680)/20; //steps_to_go;
			step_y = (parseInt(pic_height) - 146)/70; //steps_to_go;
			
			if((parseInt(pic_height) - 146 + curr_top) < 0) {
				curr_left = 0;
				curr_top = 0;
			}
			//document.getElementById("test").innerHTML = count +"; "+ picture_nbr +"; "+ pic_height +"; "+ pic_width +"; "+ e.style.top +"; "+ e.style.right +"; "+ step_x +"; "+ step_y;
			count++;

			curr_left =  0; // curr_left - step_x;
			curr_top = curr_top - step_y;
			e.style.left = parseInt(curr_left) +"px";
			e.style.top = parseInt(curr_top) +"px";
		}
	}
	setTimeout("moveHeaderPicture()", 100);
}

function startLoadNewHeaderPic() {
	setTimeout("loadNewHeaderPic()", 8000);
}

function loadNewHeaderPic() {
	picture_nbr = (picture_nbr == 1 ? 2 : 1);
	//alert(picture_nbr);
	getContent("image_front=1&picture_nbr="+ picture_nbr, "header_image");
	setTimeout("loadNewHeaderPic()", 8000);
}

function changeMakseviis(field) {
	var e = document.getElementById("makseviis");
	if(e != null && e.value == "krediitkaart") {
		showElement("krediitkaart_extra");
		return;
	}
	hideElement("krediitkaart_extra");
}

function updateEndingDate() {
	var e1 = document.getElementById("date_begin");
	var e2 = document.getElementById("date_end");
	
	if(e1 != null && e2 != null) {
		ajax_loadValue("date_end", "load.php?calculate_next_day=1&date="+ e1.value);
	}
}

function clearField(objField, strDefaultValue) {
    if(objField.value == strDefaultValue) objField.value = '';
}

function restoreField(objField, strDefaultValue) {
    if(objField.value == '') objField.value = strDefaultValue;
}

function submitContent(formName, conditions, destination) {
	var formObj = new DHTMLSuite.form({
		formRef: formName,
		action: "load.php?"+ conditions,
		responseEl: destination});

	formObj.submit();
}

function hideElement(id) {
	var e = document.getElementById(id);
	if(e != null) {
		e.style.display = "none";
	}
}

function showElement(id) {
	var e = document.getElementById(id)
	if(e != null) {
		e.style.display = "block";
	}
}
function getContent(conditions, destination) {
	ajax_loadContent(destination, "load.php?"+ conditions);
}
 
function taust(a, change) {
	a.style.backgroundColor = change;
}

function toggleElement(id) {
	var e = document.getElementById(id);
	if(e != null) {
		if(e.style.display == "none") {
			showElement(id);
		} else {
			hideElement(id);
		}
	}
}

function showGalleryPicture(id) {
	showElement("gallery_picture");
	getContent("gallery_picture=1&pic_id="+ id, "gallery_picture");
}

function printPage() {
	var e1 = document.getElementById("page_content");
	var e2 = document.getElementById("printing_page_content");

	if(e1 != null && e2 != null) {
		e2.innerHTML = e1.innerHTML;
	}
	window.print();
}

function showMovie(divId, movie) {
	showElement("show_"+ divId);
	showElement("movie_"+ divId);
	hideElement("image_"+ divId);
	var so = new SWFObject('video.swf', 'movie_'+ divId, '352', '288', '6', '#ffffff');
	so.addVariable('filePath', 'uploads/documents/'+ movie);
	so.addParam('scale', 'noscale');
	so.write('play_movie_'+ divId);
}
	
function showPicture(divId, picture) {
	showElement("show_"+ divId);
	showElement("image_"+ divId);
	hideElement("movie_"+ divId);
	document.getElementById("picture_"+ divId).src = "uploads/pictures/"+ picture;
}

function stopPlay(divId) {
	var so = new SWFObject('video.swf', 'movie_'+ divId, '352', '288', '6', '#ffffff');
	so.addParam('scale', 'noscale');
	so.write('play_movie_'+ divId);
}


function swapTopLink(id) {
	var e = document.getElementById("top_left_"+ id);
	alert(e.src);
	var e = document.getElementById("top_right_"+ id);
	alert(e.src);
	var e = document.getElementById("top_middle_"+ id);
	alert(e.background);
}

function shopAdd(id, add) {
	var e = document.getElementById("count_"+ id);
	if(e != null) {
		var newVal = parseInt(e.innerHTML) + add;
		if(newVal > 0) {
			e.innerHTML = newVal;
		}
	}
	var e = document.getElementById("count_product_"+ id);
	if(e != null) {
		var newVal = parseInt(e.innerHTML) + add;
		if(newVal > 0) {
			e.innerHTML = newVal;
		}
	}
}

function shopChartAdd(id, add) {
	var e = document.getElementById("count_chart_"+ id);
	if(e != null) {
		var newVal = parseInt(e.innerHTML) + add;
		if(newVal > 0) {
			e.innerHTML = newVal;
		} else {
			getContent("chart=1", "chart");
		}
	}
}

function raiseItem(id) {
	var e = document.getElementById(id);
	if(e != null) {
		top_item++;
		e.style.zindex = top_item;
	}
}

function openPage(id) {
	window.location.href = "index.php?id="+ id;
	return;
	getContent('navigation_top=1&id='+ id,'navigation_top');
	//getContent('navigation_second=1&id='+ id,'navigation_second');
	getContent('navigation_third=1&id='+ id,'navigation_third');
	getContent('navigation_title=1&id='+ id,'navigation_title');
	getContent('page_content=1&id='+ id,'page_content');
	getContent('image_front=1&id='+ id,'header_image');
	getContent('logo=1&id='+ id, "logo_placement");
	showMainPage();
}

function showFrontPage() {
	//hideElement("header_links");
	hideElement("navigation_title");
	getContent('navigation_top=1&id=0', 'navigation_top');
	//getContent('navigation_second=1&id=0', 'navigation_second');
	getContent('navigation_third=1&id=0', 'navigation_third');
	getContent('page_content=1&id=0', 'page_content');
	getContent('image_front=1&id=0','header_image');
}

function showMainPage() {
	showElement("header_links");
	showElement("navigation_title");
}


// calendar

function getCalendar(divId, dateObj) {
	showElement(divId);
	var e = document.getElementById(dateObj);
	var date;
	if(e != null) {
		date = e.value;
	}
	getCalendarByDate(divId, dateObj, date);
}

function getCalendarByDate(divId, dateObj, date) {
	ajax_loadContent(divId, "weebel/weebel_calendar.php?div_id="+ divId +"&form_object="+ dateObj +"&date="+ date);
}

function selectDate(divId, dateObj, date) {
	var e = document.getElementById(dateObj);
	if(e != null) {
		e.value = date;
	}
	hideElement(divId);
	
	if(dateObj == "date_begin") {
		updateEndingDate();
	}
}

function submitContent(formName, conditions) {
	var formObj = new DHTMLSuite.form({
		formRef: formName,
		action: conditions,
		responseEl: "page_content"});

	formObj.submit();
}