// 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 deleteData(url,object) {
	
	BlockMessage(3,'Da li ste sigurni da \u017Eelite izbrisati podatak?', 5, function () {
		pagesendAjax(url);
		$(object).parents('.db_row').remove();
	});	
}

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) {
			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)) {
			callback_yes.apply();
		}
	});

	$('#block_false').click(function() { 
	 	$.unblockUI();
		
		if (typeof(timeoutID) != "undefined") clearTimeout(timeoutID);
		
		if ($.isFunction(callback_no)) {
			callback_no.apply();
		}
	});
}

function pageRedirect(pageLink) {

	location.href = pageLink;
	
}

function msgBox(question,pageLink) {
	if (confirm(question)) {
		location.href = pageLink;
	}
}

function showdiv(name) {
	if (document.all[name].style.display == "none"){
			document.all[name].style.display = "";
	} else { // if is collapsed close
		document.all[name].style.display = "none";
	}
}

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","");
}

function CampareCar(id,c) {
	if (id == null) id = $("#compareList").val();
	if (c == null) c = "add";
	
	$.ajax({
		type: "GET",
		url: "auto_compare.php",
		data: c + '=' + id,
		scriptCharset: 'utf-8',
		success: function(data){
				
			$("#auto_compare").html(data);
			
			pageinit();
		}
	});
	
}

$(document).ready(function(){ 
	$.historyInit(pageloadAjax);
	
	pageinit();
	
	$("#model").change(function () {
			$.ajax({
				type: "GET",
				dataType: 'json', 
				url: "members/modelsList.php",
				data: 'model=' + $("#model").val(),
				scriptCharset: 'utf-8',
				beforeSend: function(XMLHttpRequest){
				
					$("#tip").html("<option value='0'>Ucitavanje</option>");

				},
				success: function(data){
					
					$("#tip").html("<option value='0'>Svi modeli</option>");

					for (i=0; i<data.length; i++) {
					
						if (data[i].id > 0) {

							if ($("#tip").attr("class") == data[i].id) {
								$("#tip").append("<option value='" + data[i].id + "' selected>" + data[i].naziv + "</option>");
							} else {
								$("#tip").append("<option value='" + data[i].id + "'>" + data[i].naziv + "</option>");
							}
						}
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					$("#tip").html("<option value='0'>Greška prilikom ucitavanja</option>");																
				}
			});
			
	}).change();
	
	$("#pregled_nova").click(function () {
		
		var vrsta = $("#vrsta").val();
		
		if (vrsta == "sve") {
			tmpLink = "index.php";
				
			if ($("#model").val() > 0) {
				tmpLink += "?model=" + $("#model").val();
				if ($("#tip").val() > 0) tmpLink += "&tip=" + $("#tip").val();
			}
				
			location.href = tmpLink;
		} else if (vrsta == "nova") {
			tmpLink = "index.php?sel=auto";
				
			if ($("#model").val() > 0) {
				tmpLink += "&model=" + $("#model").val();
				if ($("#tip").val() > 0) tmpLink += "&tip=" + $("#tip").val();
			}
				
			$.historyLoad(tmpLink);
		} else if (vrsta == "rabljena") {
			tmpLink = "http://www.autopijaca.ba/index.php?sel=auto";
				
			if ($("#model").val() > 0) {
				tmpLink += "&model=" + $("#model").val();
				if ($("#tip").val() > 0) tmpLink += "&tip=" + $("#tip").val();
			}
				
			//location.href = tmpLink;
			window.open(tmpLink,"_blank");
		} else if (vrsta == "salonska") {
			tmpLink = "http://www.automobili.ba/index.php?sel=auto";
				
			if ($("#model").val() > 0) {
				tmpLink += "&model=" + $("#model").val();
				if ($("#tip").val() > 0) tmpLink += "&tip=" + $("#tip").val();
			}
				
			//location.href = tmpLink;
			window.open(tmpLink,"_blank");
		} else if (vrsta == "novosti") {
			tmpLink = "index.php?sel=news";
				
			if ($("#model").val() > 0) {
				tmpLink += "&model=" + $("#model").val();
				if ($("#tip").val() > 0) tmpLink += "&tip=" + $("#tip").val();
			}
				
			$.historyLoad(tmpLink);
		} else if (vrsta == "akcije") {
			tmpLink = "index.php?sel=news&type=4";
				
			if ($("#model").val() > 0) {
				tmpLink += "&model=" + $("#model").val();
				if ($("#tip").val() > 0) tmpLink += "&tip=" + $("#tip").val();
			}
				
			$.historyLoad(tmpLink);
		}
		
		return false;
	});
	
});

