var windowSize = {
	width: 0,
	height: 0,
	
	calc: function(){
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			this.width = window.innerWidth;
			this.height = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			this.width = document.documentElement.clientWidth;
			this.height = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			this.width = document.body.clientWidth;
			this.height = document.body.clientHeight;
		}
	},
	
	getWidth: function(){
		this.calc();
		return this.width;
	},
	
	getHeight: function(){
		this.calc();
		return this.height;
	}

};


function SubmitOnEnter(e) { 
	var keynum;

	if(window.event) // IE
	  {
	  keynum = e.keyCode;
	  }
	else if(e.which) // Netscape/Firefox/Opera
	  {
	  keynum = e.which;
	  }
	if(keynum == Event.KEY_RETURN) {
		document.search_form.submit();
	}
}

function valid_mail(strEmail){
	if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	}
	else {
		return false;
	}
}

function prompt_delete(ad_name){
	if(confirm('Da li ste sigurni da želite da obrišete oglas:\n'+ad_name+'  ?')){
		return true;
	}
	else{
		return false;
	}
}
function prompt_delete_pic(){
	if(confirm('Da li ste sigurni da želite da obrišete Vaš logo ?')){
		return true;
	}
	else{
		return false;
	}
}
function prompt_delete_pic_js(el){
	alert_confirm('Da li ste sigurni da želite da obrišete Vašu sliku ?', "document.location = '"+el+"'");
	return false;
}
function go_back(){
	window.history.go(-1);
}

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 = "";

    var domainParts = document.domain.split(".");
    if( domainParts.length > 1 ){
        var domain_text = "; domain=.";
        if( domainParts.length == 2 ){
            domain_text += document.domain;
        }
        else{
            domainParts.shift();
            domain_text += domainParts.join(".");
        }
    }
    else
        var domain_text = "";
    
	document.cookie = name+"="+value+expires+"; path=/"+domain_text;
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	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;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function attach_click(el_id, color_on, color_off, class_name){
		bg_color = (typeof(class_name) == 'undefined') ? true : false;
		elem = document.getElementById(el_id);
		elements = elem.getElementsByTagName('input');

		el_count = elements.length;
		
    for(i = 0; i<el_count; i++) {
      elements[i].onclick = function(){colorize(this, color_on, color_off, class_name);};
      colorize(elements[i], color_on, color_off, class_name);
    }
}

function colorize(check_box, color_on, color_off, class_name){
	bg_color = (typeof(class_name) == 'undefined') ? true : false;
	color_on = (color_on) ? color_on : '#ccc';
	color_off = (color_off) ? color_off : '#F7F6F6';
	el_style = check_box.parentNode.parentNode;

	if(check_box.type == 'checkbox'){
		if(check_box.checked){
			if(bg_color) el_style.style.backgroundColor = color_on;
			else {		
				jQuery(el_style).toggleClass(class_name);
			}
		}
		else{
			if(bg_color) el_style.style.backgroundColor = color_off;
			else{		
				jQuery(el_style).toggleClass(class_name);
			}
		}
	}
	
}

function toggle_show_hide(el_id, type){
	if(!type) type=0;
	
	if(document.getElementById(el_id)){
		el = document.getElementById(el_id);
	}
	else{
		el = document.forms[0].elements[el_id];
	}

	if(type == 0){
		if( el.style.display == 'none' || el.style.display == '' ){
			el.style.display = 'block';
		}
		else{
			el.style.display = 'none';
		}	
	}
	else{
		if( el.style.visibility == 'hidden'){
			el.style.visibility = 'visible';
		}
		else{
			el.style.visibility = 'hidden';
		}	
	}

}

jQuery(document).ready(function() {
	colorizeChildElements('category_region_table', 'tdColored', '#dcdbda', '#f4f3f2');
	colorizeChildElements('category_region_table', 'tdTransparent', '#dcdbda', '#f4f3f2');
	colorizeChildElements('contentMiddle', 'NewJobs', '#dcdbda', '#f4f3f2');
	
	if( windowSize.getWidth() < 1120 && ( readCookie('AuthJ') == null ) ){
		jQuery('#login_container, #logged_container').css({'position': 'relative', 'right': '35px'});
	}
	
	bookmark_us();
});



function colorizeChildElements(ParentElId, ChildElClass, HoverColor, OutColor){
	if( jQuery('#' + ParentElId) ){
		var ChildElements = jQuery('.' + ChildElClass);

		ChildElements.each(function(i, Element) { 
			Element.onmouseover = function() { Element.style.backgroundColor = HoverColor };
			Element.onmouseout = function() { Element.style.backgroundColor = OutColor };
			
		});
	}
	else{
		alert('cuna');
	}
}

function alert_lakodoposla(alert_message, alert_type, enclose){
	if(!alert_type){
		my_alert_type = 'error';
	}
	else{ 
		my_alert_type = alert_type; 
	}
	
	if(!alert_type || enclose){
		alert_message = '<div class=\"errorbox\"> <div class=\"' + my_alert_type + 'box_top\"><div class=\"error_right\"> </div></div> 			<div id=\"hdr' + my_alert_type + '\"><p>' + alert_message + '</p></div> 		<div class=\"' + my_alert_type + 'box_bottom\"><div class=\"error_right\"> </div></div> 	 </div>';
	}
	
	jQuery.prompt(
    	alert_message, 
    	{ 
    		buttons:{"OK":true}, 
			show:"fadeIn",
			opacity: 0.3
		}
	);

}

var default_callbackfunc = false;
function alert_confirm(confirm_message, callbackfunc, alert_type){
	if(!alert_type){
		alert_type = 'info';
		confirm_message = '<div class=\"errorbox\"> <div class=\"' + alert_type + 'box_top\"><div class=\"error_right\"> </div></div> 			<div id=\"hdr' + alert_type + '\"><p>' + confirm_message + '</p></div> 		<div class=\"' + alert_type + 'box_bottom\"><div class=\"error_right\"> </div></div> 	 </div>';
	}
	default_callbackfunc = callbackfunc;
	
	jQuery.prompt(
    	confirm_message, 
    	{ 
    		buttons:{"OK":true, "Odustani": false},
    		callback: myformsubmit ,
			show:"fadeIn",
			opacity: 0.3
		}
	);
	
}

function myformsubmit(value, message){
	if(value){
		eval(default_callbackfunc);
	}
}

function bookmark_usx(){
	jQuery("a.jqbookmark").attr("rel","sidebar");
}
function bookmark_us(){
	
	
	// add a "rel" attrib if Opera 7+
	if(window.opera) { 
		if (jQuery("a.jqbookmark").attr("rel") == ""){ // don't overwrite the rel attrib if already set
			jQuery("a.jqbookmark").attr("rel","sidebar");
		}
	}

	jQuery("a.jqbookmark").click(function(event){
		if(!window.opera){
			event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		}
		bookmark_ldpo();
	});
}

function bookmark_ldpo(){
	var url = 'http://' + window.location.hostname;
	var title = document.title;

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external && document.all) { // IE Favorite
		window.external.AddFavorite( url, title);
	} else if(window.opera) { // Opera 7+
		return true; // do nothing - the rel="sidebar" should do the trick
	} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
		alert_lakodoposla('Kliknite CTRL+D (CMD+D na Mac-u) da bi ste dodali www.lakodoposla.com u omiljene sajtove!', 'info', true);
	}
}