// JavaScript Document

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 () { 

		$.historyLoad($(this).attr("href"));
				
		return false;
	});
	
	$("a[rel*=ajaxlink]").attr("rel","");

	$('a[rel*=lightbox]').lightBox({
		overlayOpacity: 0.6,
		containerResizeSpeed: 350
	});
	
	$("a[rel*=lightbox]").attr("rel","");
}



function formSubmitAjax(form) {
	
	$(form).ajaxSubmit({
		dataType: 'json', 
		scriptCharset: 'utf-8',
		beforeSubmit: function(a,f,o) {
			BlockMessage(1);
		},
		success: function(data) {
			
			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) {
					window.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) {
			BlockMessage(2,data.errText);
		}
	});
	
	return false;
}
var tLoading = undefined;
function pageloadAjax(url,target) {
	if (target == null) target = "mainContent";
	
	if (url) {
		//$("#"+target).html('<img src="images/loading.gif" />');
		
		var fn = function() { $("#"+target).html('<b>Stranica se u\u010Ditava </b> <img src="images/loading_dots.gif" />'); };
		tLoading = window.setTimeout(fn, 1500);
	
		if ($(window).scrollTop() > 350) $.scrollTo("#"+target,800,{offset:-50});
		
		pageTracker._trackPageview(url);
		
		$.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 ...");
			}
		});
		
	}
}
function pageload(url,target,newUrl) {
	if (target == null) target = "mainContent";
	if (newUrl == null) newUrl = "";
	
	if (newUrl && newUrl.length > 0 && $("#"+target).length <= 0) {
		target = "mainContent";
		url = newUrl;
		newUrl = "";
	}
		
	$.historyLoad(url,target,newUrl);
}

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 deleteImage(url,img,obj) {
	if (confirm('Da li ste sigurni da \u017Eelite izbrisati sliku?')) {
		
		$(obj).remove();
		
		$.ajax({
			type: "GET",
			dataType: "html",
			url: url + "&del_img=" + img,
			scriptCharset: 'utf-8',
			beforeSend: function(a,f,o) {
				$("#slika_error").html("Brisanje slike u toku ...");
			},
			success: function(data){
				
				$("#slika_error").html(data);
				
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				$("#slika_error").html(textStatus);
			}
		});

	}
}
		
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) {
						if (typeof(object) == 'string') {
							$(object).remove();
						} else {
							$(object).parents('tr:first').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 || 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 (type == 1) {
		$.blockUI({
			message: '<div align="center"><br /><img src="images/loading.gif" /><br /><br /><b>' + message + '</b><br /><br /></div>',
			css: { width: '275px' }
		});
	} 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: '275px' }
		});
	} 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: '275px' }
		});
	}
	
	if (timeout > 0) {
		if (callback_default == 0) {
			var timeoutID = setTimeout( "$.unblockUI();", (timeout * 1000));
		} else if (callback_default == 1) {
			var timeoutID = setTimeout( "$('#block_true').click();", (timeout * 1000));
		} else if (callback_default == 2) {
			var 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 pageRedirect(pageLink) {
	location.href = pageLink;
}

function msgBox(question,pageLink) {
	if (confirm(question)) {
		location.href = pageLink;
	}
}

function popUp(path,ww,hh) {
	LeftPosition=(screen.width)?(screen.width-ww)/2:100;
	TopPosition=(screen.height)?(screen.height-hh)/2:100;
	
	settings='width='+ww+',height='+hh+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';
	
	win = open(path, 'displayWindow',settings);
	win.focus();
}

function setFile(elementId) {
	str = 'ajaxfilemanager/ajaxfilemanager.php?editor=form&elementId='+elementId;
	if ($("#" + elementId).val()) str = str + "&path=" + $("#" + elementId).val();
	var win = window.open(str, 'ajaxFileImageManager', 'resizable=yes,scrollbars=yes,width=782,height=500');		
	return false;
}
function duplicate(file) {

	newFile = $("#" + file).clone().insertAfter("#" + file);
	
	newFile.removeAttr("id");
	newFile.attr("value","");
}

$(document).ready(function(){ 
	$.historyInit(pageloadAjax);
	
	$("#main_tabs a").click(function () {
		
		$("#main_tabs").children("#selected").attr("id","");
		
		$(this).attr("id","selected");

	});

	$("#main_tabs a div").click(function () {
		
		$("#main_tabs").children("#selected").attr("id","");
		
		$(this).parents("a").attr("id","selected");

	});

	$('#QuickSearch').submit(function() { 

		var actionUrl = $(this).attr("action");
		if ($("#s_firma").val() > 0) actionUrl += "&firma=" + $("#s_firma").val();
		if ($("#s_grad").val() > 0) actionUrl += "&grad=" + $("#s_grad").val();
		
		if ($("#s_kat").val() > 0) actionUrl += "&kat=" + $("#s_kat").val();
		if ($("#s_subjekt").val() > 0) actionUrl += "&subjekt=" + $("#s_subjekt").val();
		
		if ($("#s_cij1").val() > 0) actionUrl += "&cij1=" + $("#s_cij1").val();
		if ($("#s_cij2").val() > 0) actionUrl += "&cij2=" + $("#s_cij2").val();

		if ($("#s_pov1").val() > 0) actionUrl += "&pov1=" + $("#s_pov1").val();
		if ($("#s_pov2").val() > 0) actionUrl += "&pov2=" + $("#s_pov2").val();
		
		if ($("#s_spr1").val()) actionUrl += "&spr1=" + $("#s_spr1").val();
		if ($("#s_spr2").val()) actionUrl += "&spr2=" + $("#s_spr2").val();
		
		$.historyLoad(actionUrl);
		//location.href = actionUrl;

		return false; 
	});

	pageinit();
});

