// JavaScript Document

function find_class( name ) {
	var allCSSRules = document.styleSheets[0].cssRules; // przetestować dla IE
	for( var i=0 ; i<allCSSRules.length ; i++ )
		if( allCSSRules[ i ].selectorText == ("tr." + name) ) {
			return allCSSRules[ i ];
		}
	return null;
}

function hide_filter() {
	var i,j,names,show_names,sel,opt,value,disp;
	names = Array( "new_one_cnt" , "new_many_cnt" , "new_zero_cnt" , "one_cnt" , "many_cnt" , "zero_cnt" );
//	sel = document.form_select.hide_filtr;
	sel = document.getElementById("id_hide_filter");
	value = sel.options[sel.selectedIndex].value;
	if( value == "all" )        names_show = Array( "new_one_cnt"  , "new_many_cnt" , "new_zero_cnt" , "one_cnt" , "many_cnt" , "zero_cnt" );
	else if( value == "many" )  names_show = Array( "new_many_cnt" , "many_cnt" );
	else if( value == "one" )   names_show = Array( "new_one_cnt"  , "one_cnt"  );
	else if( value == "zero" )  names_show = Array( "new_zero_cnt" , "zero_cnt" );
	else if( value == "news" )  names_show = Array( "new_zero_cnt" , "new_one_cnt" , "new_many_cnt" );
	for( i=0 ; i<names.length ; i++ ) {
		disp = "none";
		for( j=0 ; j<names_show.length ; j++ ) {
			if( names_show[j] == names[i] ) {
				disp = "";
				break;
			}
		}
		find_class( names[i] ).style.display = disp;
	}
}


// szczegóły filmu w nowym oknie
function movie_detail( url ) {
	size_szer = screen.width - 60;
	size_wys  = screen.height - 60;
	x = ( screen.width - size_szer ) / 2 ;
	y = ( screen.height - size_wys ) / 2 ;		
//	url = "/movies/detail/" + mov_id;
	title = "Szczegóły filmu";
	param = "status=0,scrollbars=0,menubar=0,width=" + size_szer +",height=" + size_wys + ",left=" + x + ",top=" + y;
	window.open(url,title,param);
	}	

function getXhtmlObject() {
	if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	return false;
}

/***************************************************************************************************
Zmiana nowości przez ajax
***************************************************************************************************/
function response_change_new() {
	if( this.readyState == 4 && this.status == 200 ) {
		var is_new = this.responseText[0] == '1';
		for( var i=0 ; true ; i++ ) {
			var id_img = "img_new_" + String(i) + "_" + this.mov_id;
			var img = document.getElementById( id_img );
			if( img == null ) break;
			img.src = is_new ? "/img/new.png" : "/img/no_new.png";
		}
	}
}

function change_new( mov_id ) {
	var new_obj = getXhtmlObject();
	new_obj.onreadystatechange = response_change_new;
	new_obj.mov_id = mov_id;
	url = "http://" + window.location.hostname + "/movies/change_new/" + mov_id;
	new_obj.open( "GET" , url , true );
	new_obj.send(null);
}

function show_ordering( type , id ) {
	size_szer = screen.width - 60;
	size_wys  = screen.height - 60;
	x = ( screen.width - size_szer ) / 2 ;
	y = ( screen.height - size_wys ) / 2 ;		
	url = "http://" + window.location.hostname + "/orderings/index/" + type + "/" + id;
	title = "Sortowanie";
	param = "status=0,scrollbars=0,menubar=0,width=" + size_szer +",height=" + size_wys + ",left=" + x + ",top=" + y;
	window.open(url,title,param);	
}



/***************************************************************************************************
 Czyszczenie pod-kategorii poprzez ajax
***************************************************************************************************/
function response_clear_sub() {
	if( this.readyState == 4 ) {
		if( this.status == 200 && this.responseText == "ok" ) {
			for( var i=0 ; true ; i++ ) {
				var id_row = "sub_row_" + this.id_kat + "_" + String(i);
				var row = document.getElementById( id_row );
				if( row == null ) break;
				row.style.display="none";
			}
			alert("Operacja czyszczenia pod-kategorii zakończona sukcesem");
		} else {
			alert("Wystąpił problem czyszczenia usuwania pod-kategorii:\nstatus:" + this.statusText + "\nresponse:" + this.responseText + "\nZaleca się odświeżenie zawartości przeglądarki (CTRL+F5)");
		}
		del_wait_code();
	}
}

function clear_sub( id_kat ) {
	set_wait_code();
	var new_obj = getXhtmlObject();
	new_obj.onreadystatechange = response_clear_sub;
	new_obj.id_kat = id_kat;
	url = "http://" + window.location.hostname + "/categories/clear/" + id_kat + "/0";
	new_obj.open( "GET" , url , true );
	new_obj.send(null);
}


/***************************************************************************************************
 Usuwanie pod-kategorii poprzez ajax
***************************************************************************************************/
function response_del_sub() {
	if( this.readyState == 4 ) {
		if( this.status == 200 && this.responseText == "ok" ) {
			var row = document.getElementById(this.row_id);
			row.style.display = "none";
			alert("Operacja usuwania pod-kategorii zakończona sukcesem");
		} else {
			alert("Wystąpił problem podczas usuwania pod-kategorii:\nstatus:" + this.statusText + "\nresponse:" + this.responseText + "\nZaleca się odświeżenie zawartości przeglądarki (CTRL+F5)");
		}
		del_wait_code();
	}
}

function del_sub( sub_id , row_id ) {
	set_wait_code();
	var new_obj = getXhtmlObject();
	new_obj.onreadystatechange = response_del_sub;
	new_obj.row_id = row_id;
	url = "http://" + window.location.hostname + "/categories/delete/" + sub_id;
	new_obj.open( "GET" , url , true );
	new_obj.send(null);
}

/***************************************************************************************************
 Usuwanie kategorii głównej poprzez ajax
***************************************************************************************************/
function response_del_main() {
	if( this.readyState == 4 ) {
		if( this.status == 200 && this.responseText == "ok" ) {
			var row = document.getElementById(this.row_id);
			row.style.display = "none";
			alert("Operacja usuwania kategorii głównej zakończona sukcesem");
		} else {
			alert("Wystąpił problem podczas usuwania kategorii głównej:\nstatus:" + this.statusText + "\nresponse:" + this.responseText + "\nZaleca się odświeżenie zawartości przeglądarki (CTRL+F5)");
		}
		del_wait_code();
	}
}

function del_main( main_id ) {
	set_wait_code();
	var new_obj = getXhtmlObject();
	new_obj.onreadystatechange = response_del_main;
	new_obj.row_id = "id_main_" + main_id;
	url = "http://" + window.location.hostname + "/categories/delete/" + main_id;
	new_obj.open( "GET" , url , true );
	new_obj.send(null);
}

/***************************************************************************************************
 odłączanie filmu od pod-kategorii poprzez ajax
***************************************************************************************************/
function response_disc_movie() {
	if( this.readyState == 4 ) {
		if( this.status == 200 && this.responseText == "ok" ) {
			var row = document.getElementById(this.tbl_id);
			row.style.display = "none";
			alert("Operacja odłączania filmu od pod-kategorii zakończona sukcesem");
		} else {
			alert("Wystąpił problem podczas odłączania filmu od pod-kategorii:\nstatus:" + this.statusText + "\nresponse:" + this.responseText + "\nZaleca się odświeżenie zawartości przeglądarki (CTRL+F5)");
		}
		del_wait_code();
	}
}
function disc_movie( cat_mov_id ) {
	set_wait_code();
	var new_obj = getXhtmlObject();
	new_obj.onreadystatechange = response_disc_movie;
	new_obj.tbl_id = "id_cat_mov_" + cat_mov_id;
	url = "http://" + window.location.hostname + "/categories_movies/delete/" + cat_mov_id;
	new_obj.open( "GET" , url , true );
	new_obj.send(null);
}

/***************************************************************************************************
 trwałe usuwanie filmu i jego przyłączeń do kateorii (operacja przez ajax)
***************************************************************************************************/
function response_del_movie() {
	if( this.readyState == 4 ) {
		if( this.status == 200 && this.responseText == "ok" ) {
			var row = document.getElementById(this.row_id);
			row.style.display = "none";
			alert("Operacja kasowania filmu zakończona sukcesem");
		} else {
			alert("Wystąpił problem podczas usuwania filmu lub wpisów do bazy danych:\nstatus:" + this.statusText + "\nresponse:" + this.responseText + "\nZaleca się odświeżenie zawartości przeglądarki (CTRL+F5)");
		}
		del_wait_code();
	}
}
function del_movie( mov_id ) {
	set_wait_code();
	var new_obj = getXhtmlObject();
	new_obj.onreadystatechange = response_del_movie;
	new_obj.row_id = "id_row_mov_" + mov_id;
	url = "http://" + window.location.hostname + "/movies/delete/" + mov_id;
	new_obj.open( "GET" , url , true );
	new_obj.send(null);
}





/***************************************************************************************************
 Oczekiwanie
***************************************************************************************************/
function set_wait_code() {
	var div_wait = document.createElement("div");
	div_wait.id                    = "id_wait_code";
	div_wait.style.zIndex          = 99;
	div_wait.style.opacity         = "0.90";
	div_wait.style.position        = "fixed";
	div_wait.style.top             = "10px";
	div_wait.style.left            = "10px";
	div_wait.style.bottom          = "10px";
	div_wait.style.right           = "10px";
	div_wait.style.backgroundColor = "white";
	div_wait.style.textAlign       = "center";
	div_wait.style.verticalAlign   = "middle";
	div_wait.innerHTML             = '<img src="img/wait.gif" alt="" style="border-width:0px; margin:0px; padding:0px" />';
//	document.body.appendChild( div_wait );
	document.body.insertBefore( div_wait , document.body.firstChild );
}

function del_wait_code() {
	document.body.removeChild( document.getElementById("id_wait_code") );
}

