
(function(){
	function focusInHandler(e){
		Event.element(e).fire("focus:in");
	}
	function focusOutHandler(e){
		Event.element(e).fire("focus:out");
	}

	if (document.addEventListener){
		document.addEventListener("focus", focusInHandler, true);
		document.addEventListener("blur", focusOutHandler, true);
	} else {
		document.observe("focusin", focusInHandler);
		document.observe("focusout", focusOutHandler);
	}
})();


document.observe("dom:loaded", function() {

    var container = $(document.body);

    if (container) {
        container.observe('focus:in', function(e) {
            var el = e.element();
            if (el.match('#email')) {
                if (el.value == 'E-mail')
                    el.value = '';
            }
            if (el.match('#password')) {
                if (el.value == 'Password')
                    el.value = '';
            }
            if (el.match('#nazivkomp')) {
                if (el.value == 'Naziv firme')
                    el.value = '';
            }
            if (el.match('#pib')) {
                if (el.value == 'PIB')
                    el.value = '';
            }
            if (el.match('#adresa')) {
                if (el.value == 'Adresa')
                    el.value = '';
            }
            if (el.match('#tel')) {
                if (el.value == 'Telefon')
                    el.value = '';
            }
            if (el.match('#fax')) {
                if (el.value == 'Fax')
                    el.value = '';
            }
            if (el.match('#kont')) {
                if (el.value == 'Kontakt osoba')
                    el.value = '';
            }
            if (el.match('#napomena')) {
                if (el.value == 'Napomena')
                    el.value = '';
            }
            if (el.match('#vercode')) {
                if (el.value == 'Verifikacioni kod')
                    el.value = '';
            }


        });

        container.observe('focus:out', function(e) {
            var el = e.element();
            if (el.match('#email')) {
                if (el.value == '')
                    el.value = 'E-mail';
            }
            if (el.match('#password')) {
                if (el.value == '')
                    el.value = 'Password';
            }
            if (el.match('#nazivkomp')) {
                if (el.value == '')
                    el.value = 'Naziv firme';
            }
            if (el.match('#pib')) {
                if (el.value == '')
                    el.value = 'PIB';
            }
            if (el.match('#adresa')) {
                if (el.value == '')
                    el.value = 'Adresa';
            }
            if (el.match('#tel')) {
                if (el.value == '')
                    el.value = 'Telefon';
            }
            if (el.match('#fax')) {
                if (el.value == '')
                    el.value = 'Fax';
            }
            if (el.match('#kont')) {
                if (el.value == '')
                    el.value = 'Kontakt osoba';
            }
            if (el.match('#napomena')) {
                if (el.value == '')
                    el.value = 'Napomena';
            }
            if (el.match('#vercode')) {
                if (el.value == '')
                    el.value = 'Verifikacioni kod';
            }
        });
        
    }

});

function ValidateLoginInput(){
    returnvalue = true;
	$('errors').innerHTML = '<br/><ul>';
	if ($('email').value == '' || $('email').value == 'E-mail'){
		$('errors').innerHTML += '<li>Niste uneli E-mail adresu</li>';
		returnvalue = false;
	}
	if ($('password').value == '' || $('password').value == 'Password'){
		$('errors').innerHTML += '<li>Niste uneli lozinku</li>';
		returnvalue = false;
	}
    $('errors').innerHTML += '</ul>';

	if (returnvalue)
		$('errors').innerHTML = '';

	return returnvalue;
}

function ValidateNewCompany(isCompany){
	returnvalue = true;
	$('errors').innerHTML = 'Unos sledecih podataka je obavezan:<br/><ul>';
	if (isCompany == '1' && ($('nazivkomp').value == '' || $('nazivkomp').value == 'Naziv firme')){
		$('errors').innerHTML += '<li>Naziv komanije</li>';
		returnvalue = false;
	}
	if (isCompany == '1' && ($('pib').value == '' || $('pib').value == 'PIB')){
		$('errors').innerHTML += '<li>PIB</li>';
		returnvalue = false;
	}

    if ($('kont').value == '' || $('kont').value == 'Kontakt osoba'){
		$('errors').innerHTML += '<li>Kontakt osoba</li>';
		returnvalue = false;
	}
    
	if ($('adresa').value == '' || $('adresa').value == 'Adresa'){
		$('errors').innerHTML += '<li>Adresa</li>';
		returnvalue = false;
	}
	if ($('email').value == '' || $('email').value == 'E-mail'){
		$('errors').innerHTML += '<li>E-mail</li>';
		returnvalue = false;
	}
	if ($('tel').value == '' || $('tel').value == 'Telefon'){
		$('errors').innerHTML += '<li>Telefon</li>';
		returnvalue = false;
	}

	$('errors').innerHTML += '</ul>';
	
	if (returnvalue)
		$('errors').innerHTML = '';
		
	return returnvalue;
}

document.observe("dom:loaded", function() {
  // the element in which we will observe all clicks and capture
  // ones originating from pagination links
  var container = $(document.body);

  if (container) {

     container.observe('click', function(e) {
      var el = e.element();
      if (el.match('#willpaginate .pagination a')) {
        $('loader_div').show();
        new Ajax.Request(el.href, { method: 'get' });
        e.stop();
      }
    })
  }
})



function CloseLightBox(){
	Effect.Fade('lightbox');
	Effect.Fade('overlay');
}


var msgAnchor = null;
function showMsg(id,anchor,title, totheleft)
{
  if(msgAnchor == anchor)
  {
    hideMsg();
  }
  else
  {
    msgAnchor = anchor;
    sn = $('homeMsg');
    sn.innerHTML = '<p><b>'+ title +'</b><br/>'+ $(id).innerHTML +'</p>';
    sn.style.top = findY(anchor) + 16 + 'px';
	if (totheleft != null)
		sn.style.left = findX(anchor) - totheleft + 'px';
	else
		sn.style.left = findX(anchor) + 'px';
    sn.style.display = 'block';
  }
}

function hideMsg()
{
  sn.style.display = 'none';
  sn.style.top = '-1000px';
  sn.style.left = '-1000px';
  msgAnchor = null; 
}

var msgFixedAnchor = null;
function showFixedMsg(id,anchor)
{
  if(msgFixedAnchor == anchor)
  {
    hideFixedMsg();
  }
  else
  {
    msgFixedAnchor = anchor;
    fsn = $('fixedMsg');
	fsn.innerHTML = $(id).innerHTML;
    fsn.style.top = findY(anchor) - 50 + 'px';
	fsn.style.left = findX(anchor) + 'px';
    fsn.style.display = 'block';
  }
}

function hideFixedMsg()
{
  fsn.style.display = 'none';
  fsn.style.top = '-1000px';
  fsn.style.left = '-1000px';
  msgFixedAnchor = null; 
}

function findX(obj){
  var curleft = 0;
  if(obj.offsetParent)
    while(1) 
    {curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        obj = obj.offsetParent;}
  else if(obj.x)
    curleft += obj.x;
  return curleft;
}

function findY(obj){
  var curtop = 0;
  if(obj.offsetParent)
    while(1)
    {curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;}
  else if(obj.y)
    curtop += obj.y;
  return curtop;
}



function EnableDesktopTxt(){
    $('desktop_motherboard_text').disabled = false;
    $('desktop_processor_text').disabled = false;
    $('desktop_rammemory_text').disabled = false;
    $('desktop_graphiccard_text').disabled = false;
    $('desktop_harddisk_text').disabled = false;
    $('desktop_opticaldrive_text').disabled = false;
    $('desktop_audio_text').disabled = false;
    $('desktop_box_text').disabled = false;
    $('desktop_power_text').disabled = false;

    return true;
}

function OpenModalDetail(){
	modalwindow.open();
}

function CloseModalDetail(){
	modalwindow.close();
}

function ShowRegister(){
	$('lightboxReg').appear();$('overlay').appear();
}

function HideRegister(){
	$('lightboxReg').fade();$('overlay').fade();
}

function ShowLogin(){
	$('lightboxLog').appear();$('overlay').appear();
}

function HideLogin(){
	$('lightboxLog').fade();$('overlay').fade();
}

function ShowCompare(){
	$('lightboxComp').appear();$('overlay').appear();
}

function HideCompare(){
	$('lightboxComp').fade();$('overlay').fade();
}

function ShowCart(){
	$('lightbox').appear();$('overlay').appear();
}

function HideCart(){
	$('lightbox').fade();$('overlay').fade();
}


function recalculatescroll(){
	var scrollbar = new Control.ScrollBar('scrollbar_content','scrollbar_track');
	scrollbar.recalculateLayout();
	$('scrollbar_content').focus();
}

function recalculatescrollcomp(){
	var scrollbarcomp = new Control.ScrollBar('scrollbar_content_comp','scrollbar_track_comp');
	scrollbarcomp.recalculateLayout();
	$('scrollbar_content_comp').focus();
}
