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=/";
	//document.cookie = name+"="+value+"; path=/";
}

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 blok_cookie_toggle(x) {
    
    ck = readCookie('blok');
    ck_temp = ck.split("-");
    ck_ret = "";
    //alert(ck_temp.length);
    for(i=0; i<ck_temp.length-1; i++) {
        if(i == x) {
            if(ck_temp[i] == "NO") {
               ck_temp[i] = "YES";
            } else {
               ck_temp[i] = "NO"; 
            }
        } // if
        
        ck_ret += ck_temp[i] + "-";
        //ck_ret[ck_ret.length].replace("-");
        
    } // for
    
    //alert(ck_ret);
    
    eraseCookie('blok');
    createCookie('blok',ck_ret,'365');
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

//## Remove block ##//
function closeElx(x) {
    
    // Sacuvaj prvo u slucaju da nema cookie
    serialize();
    
    // Sakri vizuelno
    document.getElementById(x).style.display = "none";
    
    // Sredi cookie
    s1 = Url.decode(readCookie('sort1'));
    if(s1.indexOf(x) != -1) {
        
       rstring = 'sort1[]=' + x + '&';
       s1 = s1.replace(rstring,"");
       rstring_last = 'sort1[]=' + x;
       s1 = s1.replace(rstring_last,"");
       rstring_mini = 'sort1[1]=' + x + '&';
       s1 = s1.replace(rstring_mini,"");
       rstring_mini_last = 'sort1[1]=' + x;
       s1 = s1.replace(rstring_mini_last,"");
       
       eraseCookie('sort1');
       createCookie('sort1',Url.encode(s1),'365');
       location.reload(true);
       return;
    }
    
    s2 = Url.decode(readCookie('sort2'));
    if(s2.indexOf(x) != -1) {
        
       rstring = 'sort2[]=' + x + '&';
       s2 = s2.replace(rstring,"");
       rstring_last = 'sort2[]=' + x;
       s2 = s2.replace(rstring_last,""); 
       rstring_mini = 'sort2[1]=' + x + '&';
       s2 = s2.replace(rstring_mini,"");
       
       rstring_mini_last = 'sort2[1]=' + x;
       s2 = s2.replace(rstring_mini_last,"");
       
       eraseCookie('sort2');
       createCookie('sort2',Url.encode(s2),'365');
       location.reload(true);
       return;
    }
    
}

//## DODAJ TAG ##//
function add_tag_x(idx) {
    //alert(1);
    new_tag = $("#tagovi").text();
    new_tag = new_tag.split(",");
    if(new_tag.length < 5) {
        //alert(2);
        add_tag(idx);
    } else {
        //alert(3);
        document.getElementById("tag").style.display = "none";
        document.getElementById("tag_b").style.display = "none";   
    }
}

function add_tag(idx) {
    //alert(4);
    new_tag = $("#tag").get();
    $("#tagovi").load("add_tag.php?id="+idx+'&new= '+new_tag[0].value);
}

/**
* Function : dump()
* Arguments: The data - array,hash(associative array),object
*    The level - OPTIONAL
* Returns  : The textual representation of the array.
* This function was inspired by the print_r function of PHP.
* This will accept some data as the argument and return a
* text that will be a more readable version of the
* array/hash/object that is given.
*/
function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;

//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += "    ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
 for(var item in arr) {
  var value = arr[item];
 
  if(typeof(value) == 'object') { //If it is an array,
   dumped_text += level_padding + "'" + item + "' ...\n";
   dumped_text += dump(value,level+1);
  } else {
   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
  }
 }
} else { //Stings/Chars/Numbers etc.
 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
} 

/*------------------------------------------------*/
/* SIMPLE jQUERY TOOLTIP WEBSITE: 3nhanced.com    */
/*------------------------------------------------*/

$(document).ready(function() {
	$('.toolTip').hover(
		function() {
		this.tip = this.title;
		$(this).append(
			'<div class="toolTipWrapper">'
				+'<div class="toolTipTop"></div>'
				+'<div class="toolTipMid">'
					+this.tip
				+'</div>'
				+'<div class="toolTipBtm"></div>'
			+'</div>'
		);
		this.title = "";
		this.width = $(this).width();
		$(this).find('.toolTipWrapper').css({left:this.width-22})
		$('.toolTipWrapper').fadeIn(300);
	},
	function() {
		$('.toolTipWrapper').fadeOut(100);
		  //$(this).children().remove();
		  $('.toolTipWrapper').remove();
		  this.title = this.tip;
		}
	);
});


function submitenter(myfield,e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}