// JavaScript Document

var timeoutID;
var ProgressBarID;

var changeColorObj = new Array();
function changeColor(obj,state,css) {
	if (obj) {
		if (css == null) css = "div"
		if (state == 0) {
			if ($(obj).hasClass(css+"Active") == false) $(obj).removeClass(css+"Over");
		} else if (state == 1) {
			if ($(obj).hasClass(css+"Active") == false) $(obj).addClass(css+"Over");
		} else if (state == 2) {
			if ($(changeColorObj[obj.id]).length > 0) {
				$(changeColorObj[obj.id]).removeClass(css+"Over");
				$(changeColorObj[obj.id]).removeClass(css+"Active");
			}
			changeColorObj[obj.id] = obj;
			$(obj).removeClass(css+"Over");
			$(obj).addClass(css+"Active");
		}
	}
}

function pageinit() {

	$("a[@rel='ajaxlink']").click(function () { 
		if (this.target) {
			var target = this.target;
		} else {
			var target = "mainContent";
		}
		var href = $(this).attr("href");
		href = removeIndex(href);
		
		if (target == "mainContent") {
			$.history.load(href);
		} else {
			pageload(href,target);
		}
		
		return false;
	});
	
	$("a[@rel='ajaxlink']").attr("rel","");	

	$('a[rel*=facebox]').facebox({
		loading_image : 'jquery/facebox/loading.gif',
		close_image   : 'jquery/facebox/closelabel.gif'
	});
	
	$('a[rel*=facebox]').attr("rel","");	
}

function ProgressBar_request() {
	
	if ($("#progress").length > 0) {
		$.ajax({
			type: "GET",
			dataType: "json",
			url: "progressbar.php",
			scriptCharset: 'utf-8',
			success: function(data){
				
				$("#progress").width(data.perc);
				$("#progress").html(data.perc);
				$("#progress_status").html(data.status);
				
				ProgressBarID = setTimeout('ProgressBar_request()', 3000);
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#progress").width("100%");
				$("#progress").html("");
				$("#progress").html("Error: ["+textStatus+"] "+errorThrown);
			}
		});
	} else {
		if (typeof(ProgressBarID) != "undefined") clearTimeout(ProgressBarID);
	}
}

function formSubmitAjax(form,progress) {
	if (progress == null) progress = false;
	
	$(form).ajaxSubmit({
		dataType: 'json', 
		scriptCharset: 'utf-8',
		beforeSubmit: function(a,f,o) {
			
			if (progress == true) {
				BlockMessage(-1);
				
				ProgressBarID = setTimeout('ProgressBar_request()', 3000);
			} else {
				BlockMessage(1);
			}
			
		},
		success: function(data) {
			
			if (typeof(ProgressBarID) != "undefined") clearTimeout(ProgressBarID);
			
			if (data.errText) {
				
				if (data.autoHide == null) data.autoHide = 5;

				if (data.autoHide == 0) {
					BlockMessage(2,data.errText);
				} else {
					BlockMessage(2,data.errText,data.autoHide);
				}
			} else {
				if (data.reloadUrl && data.reloadUrl == true) {
					location.reload();
				} else {
					
					if (data.formUrl == "false") {
						BlockMessage(2,'Podaci sa\u010Duvani!', 3, function () {
							history.back();
						});
					} else {
						BlockMessage(3,'Podaci sa\u010Duvani!', 3, function () {
							history.back();
						}, function () {
							if (data.formUrl) $(form).attr("action",data.formUrl);
						},1);						
					}
					
				}
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			if (typeof(ProgressBarID) != "undefined") clearTimeout(ProgressBarID);
			
			BlockMessage(2,data.errText);
		}
	});
	
	return false;
}

function urlToArray(url) {
	url_array = new Array();
	
	if (url.match("\\?")) {
		tmp_array = url.split("?");
		
		url_array['_FILE'] = tmp_array[0];
		
		tmp_array = tmp_array[1].split("&");
		
		for(var i = 0; i<tmp_array.length; i++) {
			val = tmp_array[i].toString().substring(tmp_array[i].toString().indexOf("=") + 1);
			key = tmp_array[i].toString().substring(0,tmp_array[i].toString().indexOf("="));
			
			url_array[key] = val;
		}
	} else {
		url_array['_FILE'] = url;
	}
	
	return url_array;
}
var tLoading = undefined;
function pageloadAjax(url,target) {
	if (target == null) target = "mainContent";
	
	if (target == "mainContent") {
		
		get_vars = urlToArray(url);
		
		if (!url) url = "home.php";
		
		$("#buttons a").removeClass("active");
		$("#buttons_top a").removeClass("active");
		if (get_vars['_FILE'] == "movies.php") {
			if (get_vars['cat'] == "serie") {
				$("#button_3").addClass("active");
			} else if (get_vars['cat'] == "kid") {
				$("#button_4").addClass("active");
			} else if (get_vars['cat'] == "adult") {
				$("#button_5").addClass("active");
			} else {
				$("#button_2").addClass("active");
			}
			
			if (get_vars['filter'] == "new") {
				$("#button_top_1").addClass("active");
			} else if (get_vars['filter'] == "soon") {
				$("#button_top_2").addClass("active");
			}
		} else if (get_vars['_FILE'] == "members.php") {
			$("#button_6").addClass("active");
		} else if (get_vars['_FILE'] == "contact.php") {
			$("#button_7").addClass("active");
		} else {			
			$("#button_1").addClass("active");
		}
	}
	
	if (url) {
		//$("#"+target).html('<img src="images/loading.gif" />');

		var fn = function() { $("#"+target).html('<img src="images/loading.gif" />'); };
		tLoading = window.setTimeout(fn, 1500);

		if ($(window).scrollTop() > 350) $.scrollTo("#"+target,800,{offset:-50});
			
		$.ajax({
			type: "GET",
			url: url,
			scriptCharset: 'utf-8',
			success: function(html){
				clearTimeout(tLoading);
				
				$("#"+target).html(html);
				
				pageinit();
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				clearTimeout(tLoading);
				
				$("#"+target).html("<font style='color: #CC0000'>Gre\u0161ka: </font> Stranica nije u\u010Ditana ...");
			}
		});
		
		pageTracker._trackPageview(url);
		
	}
}
function pageload(url,target) {
	if (target == null) target = "mainContent";
	
	if (url == "new" || url == "soon") {
		get_vars = urlToArray(location.hash.replace(/^#/, ''));
		
		tmpUrl = "movies.php";
		if (get_vars['_FILE'] == "movies.php" && get_vars['cat']) {
			tmpUrl = tmpUrl + "?cat=" + get_vars['cat'] + "&";
		} else {
			tmpUrl = tmpUrl + "?";
		}
		url = tmpUrl + "filter=" + url;
	} else {
		get_vars = urlToArray(url);
	}
	
	if (target == "mainContent") {
		$.history.load(url);
	} else if (target == "mainData") {
		$.history.load(url,loadPageData);
	} else {
		pageloadAjax(url,target);
	}
}

function pagesendAjax(url) {
	
	$.ajax({
		type: "GET",
		dataType: "json",
		url: url,
		scriptCharset: 'utf-8',
		beforeSend: function(a,f,o) {
			BlockMessage(1);
		},
		success: function(data){
			
			if (data.errText) {
				BlockMessage(2,data.errText,5);
			} else {
				BlockMessage(2,"Zahtjev uspje\u0161no izvr\u0161en",3);
			}
			
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			BlockMessage(2,textStatus);
		}
	});
	
	return false;
}
/*
\u0107	c
\u0106	C

\u010D	c
\u010C	C

\u017D	Ž
\u017E	ž

\u0161	š
\u0160	Š

\u0111	d
\u0110	Ð
*/
function deleteData(url,object,msg) {
	
	if (msg == null) msg = 'Da li ste sigurni da \u017Eelite izbrisati podatak?';
	
	BlockMessage(3,msg,5,function () {

		$.ajax({
			type: "GET",
			dataType: "json",
			url: url,
			scriptCharset: 'utf-8',
			beforeSend: function(a,f,o) {
				BlockMessage(1);
			},
			success: function(data){
				
				if (data.delRow == null) data.delRow = 1;
				
				if (data.errText) {
					
					if (data.delRow && data.delRow == 1) {
						$(object).parents('.db_row').remove();
						
						BlockMessage(2,data.errText,3);
					} else {
						BlockMessage(2,data.errText,5);
					}
				}
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				BlockMessage(2,textStatus);
			}
		});
		
	});	
}

function BlockMessage(type,message,timeout,callback_yes,callback_no,callback_default) {
	if (type == null) type = 0;
	if (message == null) message = 'Sa\u010Dekajte trenutak ...';
	if (timeout == null) timeout = 0;
	if (callback_yes == null) callback_yes = '';
	if (callback_no == null) callback_no = '';
	
	if (callback_default == null) {
		if (type == 0 || Math.abs(type) == 1) {
			callback_default = 0;
		} else if (type == 2) { //OK
			callback_default = 1;
		} else if (type == 3) { //DA / NE
			callback_default = 2;
		}
	}
	
	if (typeof(timeoutID) != "undefined") clearTimeout(timeoutID);
	
	if (type == 0) {
		$.unblockUI();
	} else if (Math.abs(type) == 1) {
		if (type == 1) {
			$.blockUI({
				message: '<div align="center"><br /><img src="images/loading.gif" /><br /><br /><b>' + message + '</b><br /></div><br /><br />',
				css: { width: '300px' }
			});
		} else if (type == -1) {
			$.blockUI({
				message: '<div align="center"><br /><img src="images/loading.gif" /><br /><br /><b>' + message + '</b><br /><br /><div id="progress_container" style="border:1px solid #cccccc;width:250px;"><div align="left" id="progress" style="width:0%;color:#FFFFFF;background-color:#c80002;height:12px;overflow:hidden;font-size:12px;line-height:12px;"></div><div id="progress_status" style="text-align:center;font-size:12px;color:#000;"></div></div></div><br />',
				css: { width: '300px' }
			});
		}
	} else if (type == 2) { //OK
		$.blockUI({
			message: '<div align="center" style="cursor: default;"><br /><b>' + message + '</b><br /><br /><input type="button" id="block_true" value="OK / NAZAD" /><br /><br /></div>',
			css: { width: '300px' }
		});
	} else if (type == 3) { //YES NO
		$.blockUI({
			message: '<div align="center" style="cursor: default;"><br /><b>' + message + '</b><br /><br /><input type="button" id="block_true" value="DA" /> <input type="button" id="block_false" value="NE" /><br /><br /></div>',
			css: { width: '300px' }
		});
	}
	
	if (timeout > 0) {
		if (callback_default == 0) {
			timeoutID = setTimeout( "$.unblockUI();", (timeout * 1000));
		} else if (callback_default == 1) {
			timeoutID = setTimeout( "$('#block_true').click();", (timeout * 1000));
		} else if (callback_default == 2) {
			timeoutID = setTimeout( "$('#block_false').click();", (timeout * 1000));
		}
	}
	
	$('#block_true').click(function() { 
	 	$.unblockUI();
		
		if (typeof(timeoutID) != "undefined") clearTimeout(timeoutID);

		if ($.isFunction(callback_yes)) {
			$('#block_true').unbind("click");
			$('#block_false').unbind("click");
			callback_yes.apply();
		}
	});

	$('#block_false').click(function() { 
	 	$.unblockUI();
		
		if (typeof(timeoutID) != "undefined") clearTimeout(timeoutID);
		
		if ($.isFunction(callback_no)) {
			$('#block_true').unbind("click");
			$('#block_false').unbind("click");
			callback_no.apply();
		}
	});
}

function PopUp(url,ww,hh) {
	winww = ww;
	winhh = (hh * ww) / ww;

	LeftPosition=(screen.width)?(screen.width-winww)/2:100;
	TopPosition=(screen.height)?(screen.height-winhh)/2:100;
	
	settings='width='+winww+',height='+winhh+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	
	win = open(url, 'displayWindow',settings);
	win.focus();
}

$(document).ready(function(){ 
	$.history.init(pageloadAjax);		
	
	pageinit();	
});
