﻿function backToTop() {
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;

	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}

	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}

	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));

	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

	if (x > 0 || y > 0) {
		window.setTimeout("backToTop()", 25);
	}
}


$(document).ready(function() {

	$("a.top").click(function() {
		backToTop();
		return false;
	});

	$(".lang-mid ul li.lang-selected").click(function() {
		$('.lang-mid ul li.lang-off').toggle();
		return false;
	});

	$(".lang-mid ul").blur(function() {
		$('.lang-mid ul li.lang-off').toggle();
		return false;
	});
});



function PopUpGrowl(s) {
	$.growlUI(null, s, null, null);
}

$(document).ready(function() {
	$(".searchbox").focus(function() {
		$(this).parent().css("background-position", "0 -29px");
		$(this).css("color", "#233e85")
	});
	$(".searchbox").blur(function() {
		$(this).parent().css("background-position", "0 0");
		$(this).css("color", "#666666");
	});
});

$(document).ready(function() {
	$('input.search-go').hover(function() {
		this.src = '/WebResources/img/search-go-hover.gif';
	}, function() {
		this.src = '/WebResources/img/search-go.gif';
	});

	$("div.news-item").hover(
		function() {
			$(this).addClass("selected-news");
		},
		function() {
			$(this).removeClass("selected-news");
		});

	$("div.news-item").click(function() {
		window.location = $(this).find("a:first").attr("href");
	});

});

$(document).ready(function() {
	$("div.news-big").hover(
		function() {
			$(this).find('h3').css("color", "#c00b0f");
			$(this).find('p').css("color", "#000000");
			$(this).find('a.comments').css("color", "#696969");
			$(this).find('a.full-news').css("color", "#233e85");
			$(this).find('a.full-news').css("background-position", "bottom right");
		},
		function() {
			$(this).find('h3').css("color", "#233e85");
			$(this).find('p').css("color", "#666666");
			$(this).find('a.comments').css("color", "#999999");
			$(this).find('a.full-news').css("color", "#c20c0f");
			$(this).find('a.full-news').css("background-position", "top right");
		});
});

$(document).ready(function() {
	$('.ta').click(
function() {
	if (this.value == this.defaultValue) {
		this.value = '';
	}
}
);
	$('.ta').blur(
function() {
	if (this.value == '') {
		this.value = this.defaultValue;
	}
}
);
});



$(document).ready(function() {

	var state = 'show';

	$('div.infobox-body').show(0);

	state = readCookie('infobox-cookie');

	if (state) {
		if (state.trim() == 'show') {
			$('div.infobox-body').show(0);
		}
		if (state.trim() == 'hide') {
			$('div.infobox-body').hide(0);
		}
	}

	function createCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
			var expires = "; expires=" + date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		try {
			var ca = document.cookie.split(';');
		} catch (err) {
			return false;
		}
		for (var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) == ' ') c = c.substring(1, c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
		}
		return null;
	}

	$('a.infobox-open').click(function() {
		$('div.infobox-body').show(0);
		createCookie('infobox-cookie', 'show', 7);
		return false;
	})

	$('a.infobox-close').click(function() {
		$('div.infobox-body').hide(0);
		createCookie('infobox-cookie', 'hide', 7);
		return false;
	})

});

function listbox_move(listID, direction) {

	var listbox = document.getElementById(listID);
	var selIndex = listbox.selectedIndex;

	if (-1 == selIndex) {
		alert("Изабери барем један садржај, који желиш померити!");
		return;
	}

	var increment = -1;
	if (direction == 'up')
		increment = -1;
	else
		increment = 1;

	if ((selIndex + increment) < 0 ||
		(selIndex + increment) > (listbox.options.length - 1)) {
		return;
	}

	var selValue = listbox.options[selIndex].value;
	var selText = listbox.options[selIndex].text;
	listbox.options[selIndex].value = listbox.options[selIndex + increment].value
	listbox.options[selIndex].text = listbox.options[selIndex + increment].text

	listbox.options[selIndex + increment].value = selValue;
	listbox.options[selIndex + increment].text = selText;

	listbox.selectedIndex = selIndex + increment;
}

function listbox_moveacross(sourceID, destID, direction) {
	var src = document.getElementById(sourceID);
	var dest = document.getElementById(destID);

	if (dest.length > 3 && direction == 'right') {
		alert("Можете изабрати највише 4 садржаја!");
	}
	else {
		if (src.length == 0 && direction == 'left') {
			alert("Не постоји ни један одабрани садржај!");
		}
		else {

			for (var count = 0; count < src.options.length; count++) {

				if (src.options[count].selected == true) {
					var option = src.options[count];
					var newOption = document.createElement("option");
					newOption.value = option.value;
					newOption.text = option.text;
					newOption.selected = true;
					try {
						dest.add(newOption, null); //Standard
						src.remove(count, null);
					} catch (error) {
						dest.add(newOption); // IE only
						src.remove(count);
					}
					count--;
				}
			}
		}
	}
}

$(document).ready(function() {
	$("li.usluga-vise").hover(
	  function() {
		  $(this).children("ul.jezici").show();
	  },
	  function() {
		  $(this).children("ul.jezici").hide();
	  }
	);


	var iconJedan = $('span.pr-icon1 > span').height();
	var iconDva = $('span.pr-icon2 > span').height();
	if (iconJedan > 24) {
		$('span.pr-icon2 > span').height(iconJedan);
	}
	if (iconDva > 24) {
		$('span.pr-icon1 > span').height(iconDva);
	}

});


$(document).ready(function() {


	$('div.input-large input').focus(function() {
		if ($(this).val() == $(this)[0].defaultValue) {
			$(this).val("");
		}
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).val($(this)[0].defaultValue);
		}
	});

});

$(document).ready(function() {
	if (window.location.href.indexOf("institucija") >= 0) {
		$("#btnServices").addClass("inst-active");
	}
	if (window.location.href.indexOf("pitajte_nas") >= 0) {
		$("#btnQuestions").addClass("inst-active");
	}
	if (window.location.href.indexOf("dokumenta") >= 0) {
		$("#btnDocuments").addClass("inst-active");
	}
});

function ShowModal(url) {
	$.colorbox({ href: url, iframe: true, width: "40%", height: "40%", scrolling: false });
}

function ShowModalScrolable(url) {
	$.colorbox({ href: url, iframe: true, width: "70%", height: "70%", scrolling: true
	});
}

function Read(textToRead) {

	$.ajax({
		type: "POST",
		async: true,
		url: "../../../App_Modules__SnTRS.eUpravaRSCms.Modules__SnTRS.eUpravaRSCms.Modules.TextToSpeechModule.TTSService.asmx/GetStream",
		data: "{'inputText': '" + textToRead + "'}",
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		success: function(result) {

			$("#jplayer").jPlayer({
				ready: function() {
					$(this).jPlayer("setMedia", {
						mp3: result.d
					}).jPlayer("play");
				},
				swfPath: "/WebResources/code/jPlayer",
				supplied: "mp3"
			});
		},
		error: function(xhr, status, error) {
			var err = eval("(" + xhr.responseText + ")");
		}
	});
};

var PluginInfo = {
	Description: null,
	Filename: null,
	Name: null,
	IsInstalled: false
};

function DetectPDF() {	

	var found = false;
	var info = 'NotInstalled';

	try {
		acrobat4 = new ActiveXObject('PDF.PdfCtrl.1');
		if (acrobat4) {
			found = true;
			info = 'v. 4.0';
			PluginInfo.IsInstalled = true;
		}
	}
	catch (e) {
		//???
	}

	if (!found) {
		try {
			control = new ActiveXObject('AcroPDF.PDF.1');
			if (control) {
				found = true;
				//info = 'v. 7+';
				version = control.GetVersions().split(',');
				version = version[0].split('=');
				version = parseFloat(version[1]);
				info = version;
				PluginInfo.IsInstalled = true;
			}
		}
		catch (e) {
			//???
		}

		var i;
		if (!found && navigator.plugins && navigator.plugins.length > 0) {
			for (i = 0; i < navigator.plugins.length; i++) {
				if (navigator.plugins[i].name.indexOf('Adobe Acrobat') > -1) {
					found = true;					
					break;
				}
				if (navigator.plugins[i].name.indexOf('Chrome PDF Viewer') > -1) {
					found = true;
					
					break;
				}
			}
			if (found) {

				info = navigator.plugins[i].description + ' (' + navigator.plugins[i].filename + ')';
				PluginInfo.Description = navigator.plugins[i].description;
				PluginInfo.Filename = navigator.plugins[i].filename;
				PluginInfo.IsInstalled = true;
				PluginInfo.Name = navigator.plugins[i].name;
			}
		}
	}
	
	
	return PluginInfo;
}

var browser_info = {
	name: null,
	acrobat: null,
	acrobat_ver: null,
	browser: null,
	browserVersion: null
};

function DetectCookies() {

	document.cookie = "testcookie"
	cookieEnabled = (document.cookie.indexOf("testcookie") != -1);	
	return cookieEnabled;
}

function DetectJavaRuntime() {
	
    if (typeof deployJava != "undefined") {
		javaVersion = deployJava.getJREs()[0];
		if (typeof javaVersion == "undefined") { 			
		    return [false, "undefined"];            
		}
		javaVersion = parseFloat(javaVersion.substring(0, 3));
		if (javaVersion < 1.5) {
			return [false, javaVersion];            
		} else {
			return [true, javaVersion];
		}
	}

}

function DetectBrowserVersion() {
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
	var version = 0;

	// Is this a version of IE?
	if ($.browser.msie) {
		userAgent = $.browser.version;
		userAgent = userAgent.substring(0, userAgent.indexOf('.'));
		version = userAgent;
		browser_info.browser = "IE";
		browser_info.browserVersion = version;

	}

	// Is this a version of Chrome?
	if ($.browser.chrome) {
		userAgent = userAgent.substring(userAgent.indexOf('chrome/') + 7);
		userAgent = userAgent.substring(0, userAgent.indexOf('.'));
		version = userAgent;
		// If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
		$.browser.safari = false;
		browser_info.browser = "Chrome";
		browser_info.browserVersion = version;
	}

	// Is this a version of Safari?
	if ($.browser.safari) {
		userAgent = userAgent.substring(userAgent.indexOf('safari/') + 7);
		userAgent = userAgent.substring(0, userAgent.indexOf('.'));
		version = userAgent;
		browser_info.browser = "Safari";
		browser_info.browserVersion = version;
	}

	// Is this a version of Mozilla?
	if ($.browser.mozilla) {
		//Is it Firefox?
		if (navigator.userAgent.toLowerCase().indexOf('firefox') != -1) {
			userAgent = userAgent.substring(userAgent.indexOf('firefox/') + 8);
			userAgent = userAgent.substring(0, userAgent.indexOf('.'));
			version = userAgent;
			if (($.browser.version).substring(0, 5) == '1.9.1') {
				version = "3.5+";
			}
			if (($.browser.version).substring(0, 5) == '1.9.2') {
				version = "3.6+";
			}
			browser_info.browser = "Firefox";
			browser_info.browserVersion = version;
		}
		// If not then it must be another Mozilla
		else {
		}
	}

	// Is this a version of Opera?
	if ($.browser.opera) {
		userAgent = userAgent.substring(userAgent.indexOf('version/') + 8);
		userAgent = userAgent.substring(0, userAgent.indexOf('.'));
		version = userAgent;
		browser_info.browser = "Opera";
		browser_info.browserVersion = version;
	}
	return browser_info;
}
jQuery(document).ready(function() {

	var currentYear = (new Date).getFullYear();
	var options = '';
	for (var i = currentYear; i >= currentYear - 5; i--) {
		options += '<option value="' + i + '">' + i + '.</option>';
	}

	jQuery('select[name*=archiveYear]').append(options);

});

function searchArchive() {
	var selectedMonth = jQuery('#month').val();
	var selectedYear = jQuery('#year').val();

	if (!isNaN(selectedMonth) && !isNaN(selectedYear)) {
		var selectedDate = new Date();
		selectedDate.setFullYear(selectedYear, selectedMonth - 1, 1);

		var endDate = new Date();
		endDate.setFullYear(selectedYear, selectedMonth, 1);

		var searchFrom = selectedDate.getFullYear() + '-' + (selectedDate.getMonth() + 1) + '-01';
		var searchTo = endDate.getFullYear() + '-' + (endDate.getMonth() + 1) + '-01';

		var url = location.href.replace(window.location.search, '');

		location.href = url + '?searchFrom=' + searchFrom + '&searchTo=' + searchTo;
	}
}


function getParameterByName(name) {
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);

	if (results == null)
		return "";
	else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
}
