/**
* Copyright fluena.com
* author: agvozden@gmail.com 
*/
$().ready(function() {
	/**
	 * napravi combo box-ove
	 * ovo mora na pocetku da bi pohvatao evente
	 */
	$(".combo").each(function(){
		var elem = $(this);
		var elem_id = elem.attr('id');
		var elem_value = $("#" + elem_id + " option:selected").text();
		var elem_width = elem.width() + 'px';
		if (elem_width == '0px') elem_width = elem.css('width');
		var attr_r = elem[0].attributes;
		var elem_attrs = '';
		for(var i=0;i<attr_r.length;i++) {
			if (attr_r[i].nodeValue!==null && attr_r[i].nodeValue!==false && attr_r[i].nodeValue!=='' && attr_r[i].nodeValue!==0) 
				elem_attrs += ' ' + attr_r[i].nodeName + '="' + attr_r[i].nodeValue + '"';
		}
		elem.replaceWith('<span onclick="combobox(\'' + elem_id + '\');" class="combobox">'
		+ '<input id="' + elem_id + '_mask" type="text" value="' + elem_value + '" readonly="readonly" style="width:' + elem_width + '" />'
		+ '<span><select' + elem_attrs + '>'
		+ elem.html() + '</select></span></span>');		
	});
	//
	$("a").focus(function(){
		$(this).blur();
	});
	$("select#view").blur(function(){
		window.location = url + '?view=' + ($("select#view option:selected").val());  
	});	
});

/**
* common libraries
*/
/**
* tabber object
*/
function tabber(){
	tabber_tabs:new Array();
	attrName:'class';
	this.show = function(elemId){
		for (var elem_id in tabber_tabs){
			tab_i = document.getElementById(tabber_tabs[elem_id]);
			if (tab_i) tab_i.style.display = "none";
			tab_i = document.getElementById('caption_' + tabber_tabs[elem_id]);
			if (tab_i) tab_i.setAttribute(tabber.attrName, 'none');
		}
		tab_i = document.getElementById(elemId);
		if (tab_i) tab_i.style.display = "block";
		tab_i = document.getElementById('caption_' + elemId);
		if (tab_i) tab_i.setAttribute(tabber.attrName, 'current');
	};
	this.populate = function(tabs){
		if (document.all) {
			tabber.attrName = 'className';
			if (navigator.appName=="Microsoft Internet Explorer"){
				var index = navigator.appVersion.indexOf('MSIE');				
				if (index != -1 && (parseFloat(navigator.appVersion.substring(index+4))>7)){
					tabber.attrName = 'class';
				}	
			}
		} else {
			tabber.attrName = 'class';
		}
		tabber_tabs = tabs;
		this.show(tabber_tabs[0]);
	};
};


/**
 * krpljenje za id za ie
 */
if (/msie/i.test (navigator.userAgent)){ //only override IE
	document.nativeGetElementById = document.getElementById; 
	document.getElementById = function(id){
		var elem = document.nativeGetElementById(id);
		if(elem){
			//make sure that it is a valid match on id
			if(elem.attributes['id'].value == id){
				return elem;
			}else{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++){
					if(document.all[id][i].attributes['id'].value == id){
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	}
}
/**
 * combobox emulation
 */
var combobox = function(elemname){
	var elem = document.getElementById(elemname);
	var holder = document.getElementById(elemname+'_mask');
	var dd = elem.selectedIndex;
	var ss = elem[dd].text; 
	holder.active = true;
	if (elem.style.display == 'block'){
		elem.size = 0;
		elem.style.display = 'none';
		elem.blur();
		holder.value = ss;
	} else {
		if (!elem.length || elem.length<2) return false;
		elem.style.display = 'block';
		var el_len = elem.length;
		if (el_len>9) el_len=9;
		elem.size = el_len;
		try{elem.focus();} catch (err) {}		
		$(holder).mouseover(function () {
			holder.active = true;
		});
		$(holder).mouseout(function () {
			holder.active = false;
		});
		$(elem).blur(function () {
			if (holder.active) return;
			$(this).css('display','none').size = 0;
		});    	
	}
};


/**
 * jquery.hint.js
 */
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};

