var scrn_x;
var scrn_y;

var showed = 1;
var lookUpState;
var galery_URL = "http://www.edinenie-bg.com/galery"
var view;
var type;

var description_box;
var description_show = 0;
var description_height;

var hid2;
var shown2;

var comment_box;
var comment_height;

var hid;
var shown;

var add_comment_box;
var add_comment_show = 0;
var add_comment_height;

var hid3;
var shown3;

function init_galery(){
	view = getURLVar("view");
	type = getURLVar("type");

	if(document.getElementById('galery_holder')){
		document.getElementById('galery_holder').parentNode.style.textAlign = "center";	
	}
	
	if(document.getElementById('counter')){
		document.getElementById('counter').onclick = clearVal;
	}
	
	if(document.getElementById('chPic')){
		document.getElementById('chPic').onclick = chPic;
	}
		//document.getElementById('search_btn').onclick = show_search;

	if(document.getElementById('submit_comment')){document.getElementById('submit_comment').onclick = checkData;}
		
	if(document.getElementById('description_hider')){document.getElementById('description_hider').onclick = description_handler;}
	if(document.getElementById('add_comment_hider')){
	document.getElementById('add_comment_hider').onclick = add_comment_handler;
	}
	
		if(document.getElementById('user_comments') && document.getElementById('comment_hider')){
		comment_box =  document.getElementById('user_comments').style;
		comment_box.display = "none"; 
		comment_box.height = document.getElementById('user_comments').style.height = "20px"; 
		comment_box.overflow = "hidden"; 
		comment_height = document.getElementById('user_comments').style.height;
		
		if(	document.getElementById('comment_hider')){
			document.getElementById('comment_hider').onclick = comments_handler;
		}
		}
		
		if(document.getElementById('description')){
		description_box = document.getElementById('description').style;
		description_box.height = "1px";
		description_box.display = "none";
		description_height = parseInt(description_box.height);
		}
		
		if(document.getElementById('add_comment')){
		add_comment_box = document.getElementById('add_comment').style;
		add_comment_box.overflow = "hidden"; 		
		add_comment_box.display = "none"; 		
		add_comment_box.height = "0px";
		add_comment_height = parseInt(add_comment_box.height);
		}

	
	
	
	for(var op=0;op<document.getElementsByTagName('input').length;op++){
		if(document.getElementsByTagName('input')[op].className == "pager"){
			document.getElementsByTagName('input')[op].onclick = pages;
		}
	}
}
	
function delConfirm(DelId){
	if(confirm('Изтриване')){
		window.location = DelId;
	}else{
		window.location = '#';
	}
}

function confirmation(msg,url){
	var answ = confirm(msg);
	if(answ){
		window.location = url;
	}else{
		return false;
	}
}

function show_search(){
	if(document.getElementById("search_panel").style.display == "block"){
		document.getElementById("search_panel").style.display = "none";
	}else{
		document.getElementById("search_panel").style.display = "block";
	}
}

function hider2(){
	if(parseInt(comment_box.height) > 20){
		comment_height = comment_height - 10;
		comment_box.height = comment_height + "px";
	}else{
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		comment_height = parseInt(comment_box.height);
		comment_box.display= "none";
	}
}

function shower2(){
	if(parseInt(comment_box.height) < 1100){
		comment_height = comment_height + 10;
		comment_box.height = comment_height + "px";
	}else{
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		comment_height = parseInt(comment_box.height);
	}
}

function comments_handler(){
	comment_height = parseInt(comment_box.height);
	if(parseInt(comment_box.height) > 20){
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		document.getElementById('comment_hider').value = "Коментари";
		hid = setInterval('hider2();',2);
	}else {
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		document.getElementById('comment_hider').value = "Скрий Коментари";
		comment_box.display= "block";
		document.body.style.overflow = "auto";
		shown = setInterval('shower2();',2);
	}

}

function description_hider(){
	if(parseInt(description_box.height) > 1){
		description_height = description_height - 5;
		description_box.height = description_height + "px";
	}else{
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		description_height = parseInt(description_box.height);
		description_box.display = "none";
	}
}

function description_shower(){
	if(parseInt(description_box.height) < 250){
		description_height = description_height + 5;
		description_box.height = description_height + "px";
	}else{
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		description_height = parseInt(description_box.height);

	}
}

function description_handler(){
	description_height = parseInt(description_box.height);
	if(parseInt(description_box.height) > 1){
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		hid3 = setInterval('description_hider();',2);
		document.getElementById('description_hider').value = "Описание";
		document.body.style.overflow = "hidden";
	}else {
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		description_box.display = "block";
		document.body.style.overflow = "auto";
		shown3 = setInterval('description_shower();',2);
		document.getElementById('description_hider').value = "Скрий Описание";
	}

}

function add_comment_hider(){
	if(parseInt(add_comment_box.height) > 1){
		add_comment_height = add_comment_height - 5;
		add_comment_box.height = add_comment_height + "px";
	}else{
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		document.getElementById('add_comment_hider').value = "Добави Коментар";
		add_comment_box.display = "none"; 	
	}
}

function add_comment_shower(){
	if(parseInt(add_comment_box.height) < 250){
		add_comment_box.display = "block"; 		
		document.getElementById('add_comment_hider').value = "Добави Коментар";
		add_comment_height = add_comment_height + 5;
		add_comment_box.height = add_comment_height + "px";
	}else{
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		add_comment_box.display = "block"; 	
		add_comment_height = parseInt(add_comment_box.height);
	}
}

function add_comment_handler(){

	add_comment_height = parseInt(add_comment_box.height);
	if(parseInt(add_comment_box.height) > 1){
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		hid2 = setInterval('add_comment_hider();',2);
		document.body.style.overflow = "hidden";
	}else {
		clearInterval(hid3);	
		clearInterval(shown3);
		clearInterval(hid2);	
		clearInterval(shown2);
		clearInterval(hid);	
		clearInterval(shown);
		add_comment_box.display = "block"; 		
		document.body.style.overflow = "auto";
		shown2 = setInterval('add_comment_shower();',2);
	}

}

function pages(){
	hide(showed);
	document.getElementById("page_"+this.value).style.display = "block";
	showed = this.value;
}
	
function hide(showed){
	document.getElementById("page_"+showed).style.display = "none";
}

function checkData(){
	if(document.getElementById('user_name').value != "" && document.getElementById('comment').value != "" && document.getElementById('securityCode').value != ""){
		document.forms[0].submit();
	}else{
		alert('Моля попълнете полетата Име и Коментар, както и кода от картинката !');
		return false;
	}
}

function xDocSize(){
  var b=document.body, e=document.documentElement;
  var esw=0, eow=0, bsw=0, bow=0, esh=0, eoh=0, bsh=0, boh=0;
  if (e) {
    esw = e.scrollWidth;
    eow = e.offsetWidth;
    esh = e.scrollHeight;
    eoh = e.offsetHeight;
  }
  if (b) {
    bsw = b.scrollWidth;
    bow = b.offsetWidth;
    bsh = b.scrollHeight;
    boh = b.offsetHeight;
  }
  alert('compatMode: ' + document.compatMode + '\n\ndocumentElement.scrollHeight: ' + esh + '\ndocumentElement.offsetHeight: ' + eoh + '\nbody.scrollHeight: ' + bsh + '\nbody.offsetHeight: ' + boh + '\n\ndocumentElement.scrollWidth: ' + esw + '\ndocumentElement.offsetWidth: ' + eow + '\nbody.scrollWidth: ' + bsw + '\nbody.offsetWidth: ' + bow);
  return {w:Math.max(esw,eow,bsw,bow),h:Math.max(esh,eoh,bsh,boh)};
}

function zoomer(){
window.onresize = zoomer;
window.onFocus = zoomer;

  var b=document.body, e=document.documentElement;
  var scrn_x=0,scrn_x2=0, scrn_y=0, scrn_y2=0;
  
  if (e) {
    scrn_x = e.scrollWidth;
    scrn_x2 = e.offsetWidth;
    scrn_y = e.scrollHeight;
    scrn_y2 = e.offsetHeight;
  }
  if (b) {
    scrn_x = b.scrollWidth;
    scrn_x2 = b.offsetWidth;
    scrn_y = b.scrollHeight;
    scrn_y2 = b.offsetHeight;
	
  }
  
	var TRimg = new Image();
	TRimg.src = document.getElementById('TR_pic').src;
	var TR_x = TRimg.width;
	var TR_y = TRimg.height;

	
	if(document.getElementById('sort')){
		document.getElementById('sort').style.display = "none";
	}
	
		
	if(scrn_y > TR_y || scrn_x > TR_x){
		if(scrn_y < 600){
			TR_y = 850;
		}
	
	document.getElementById('pic_holder').style.top = 25 + "px";
	
	
		if(navigator.appName == "Opera"){
			document.getElementById('transparent').style.width = scrn_x + "px";
		}else{
			document.getElementById('transparent').style.width = scrn_x + 14 +  "px";
		}
		

		
		
		if(document.all && navigator.appName == "Microsoft Internet Explorer"){
			 if(TR_y + 300 > scrn_y){
				document.getElementById('transparent').style.paddingBottom = (TR_y - 1) + "px";	
				document.getElementById('pic_holder').style.paddingBottom =  20 + "px";	
			}else{
				document.getElementById('transparent').style.height = scrn_y + "px";	
			}
			
		}else if(navigator.appName == "Opera"){
		
		if(TR_y  > scrn_y){
				document.getElementById('transparent').style.paddingBottom = (TR_y + 113) + "px";	
				document.getElementById('pic_holder').style.paddingBottom =  0 + "px";	
			}else{
				document.getElementById('transparent').style.paddingBottom = (scrn_y) + "px";	
			}
			
		}else{
			document.getElementById('transparent').style.height = "100%";	
		}
		
		document.getElementById('pic_holder').style.left = (scrn_x - TR_x)/2 + "px";

		
	}else{
	
		document.getElementById('transparent').style.height = TR_y + 100 + "px";
		document.getElementById('transparent').style.width = TR_x + 50 + "px";

		document.getElementById('pic_holder').style.top = "50px";
		document.getElementById('pic_holder').style.left = "25px";
		document.getElementById('pic_holder').style.bottom = "50px";
	
	}
	

}
	
function getURLVar(urlVarName) {
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1]){
	var urlVars = urlHalves[1].split('&');

	for(var i=0; i<=(urlVars.length); i++){
		if(urlVars[i]){
			var urlVarPair = urlVars[i].split('=');
			if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
				urlVarValue = urlVarPair[1];
			}
		}
	}
	}
		
	return urlVarValue;   
}
	
function chPic(){
		var picToShow = document.getElementById('counter').value;
		var all_pics = document.getElementById('all_pics').value;
		var sorting;
		
		if(getURLVar("sort") == "asc" || getURLVar("sort") == "desc"){
			sorting = "&criteria="+getURLVar("criteria")+ "&sort=" +getURLVar("sort");
		}else{
			sorting ="";
		}
		
		var phpc_event_id = getURLVar('phpc_event_id');

	
	if(lookUpState){		
		if(parseInt(picToShow)){
				
		if(picToShow > all_pics  && picToShow > 10){
			alert('Няма снимка с този номер');
			picToShow = 1;
		}
		
		if(view=="thumbs"){
			if(type == "pan"){
				window.location = galery_URL+"/?view=thumbs&type=pan&page=" + picToShow + sorting;
			}else{
				window.location = galery_URL+"/?view=thumbs&page=" + picToShow + sorting;
			}
		}else if(view == "phpc_event"){
				window.location = galery_URL+"/?view=phpc_event&phpc_event_id="+ phpc_event_id +"&id=" + picToShow + sorting;
		}else{
			if(type == "pan"){
				window.location = galery_URL+"/?type=pan&id=" + picToShow + sorting;
			}else{
				window.location = galery_URL+"/?id=" + picToShow + sorting;
			}
		}
		
		}
			
	}
		lookUpState = 0;
	}

function clearVal(){
	document.getElementById('counter').value = "";
	lookUpState = 1;
}

function sort(){
	var type  = getURLVar('type');
	var view  = getURLVar('view');
	var id  = getURLVar('id');
	var page  = getURLVar('page');
	var phpc_event  = getURLVar('view');
	var phpc_event_id  = getURLVar('phpc_event_id');
	
	
	
	if(document.getElementById('sort').value != false){
	if(view == "thumbs"){
		if(type == "pan"){
			window.location = galery_URL + "?view=thumbs&type=pan&page=" + page + "&criteria=" + document.getElementById('sort').value;
		}else{
			window.location = galery_URL + "?view=thumbs&page="+ page + "&criteria=" + document.getElementById('sort').value;		
		}
	}else{
	if(phpc_event == "phpc_event"){
		window.location = galery_URL + "/index.php?view=phpc_event&phpc_event_id="+ phpc_event_id + "&id=" + id + "&criteria=" + document.getElementById('sort').value;	
	}else if(phpc_event == "phpc_events"){
		window.location = galery_URL + "/index.php?view=phpc_events&page="+page;
	}else{
		if(type == "pan"){
			window.location = galery_URL + "?id="+ id + "&type=pan" + "&criteria=" + document.getElementById('sort').value;		
		}else{
			window.location = galery_URL + "?id="+ id + "&criteria=" + document.getElementById('sort').value;				
		}
	}
	}
	}else{
		alert('Изберете критерия');
	}


}
