//$(document).ready(function() {

//ova funkcija clear search pojle na focus i vraca default value na lose focus
//ukoliko zelimo da druge input polja imaju ovu funkcijalnost onda na input tag treba dodati class="clear_input"
//$.fn.search = function() {
//	return this.focus(function() {
//		if( this.value == this.defaultValue ) {
//			this.value = "";
//		}
//	}).blur(function() {
//		if( !this.value.length ) {
//			this.value = this.defaultValue;
//		}
//	});
//};
//$(".clear_input").search();




//content extra tabs
//	$('#container-1').tabs();
//	$('#container-2').tabs();
//	$('#container-3').tabs();
//	$('#container-4').tabs();
//	$('.nav_extra-1 a').click();
//	return false;


//});

///////////////////////////////////////////////////////////////////////////////////
// Fancy checkboxes and radio bottons

//preload
//var preloaded = new Array();
//function preload_images() {
//    for (var i = 0; i < arguments.length; i++){         
//        preloaded[i].setAttribute('src',arguments[i]);
//    };
//};
//preload_images(
//		'/images/ico/radio_off.gif',
//		'/images/ico/check_off.gif',
//		'/images/ico/check_on.gif',
//		'/images/ico/radio_on.gif'
//);

var d = document;
var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
var gebtn = function(parEl,child) { return parEl.getElementsByTagName(child); };
onload = function() {
    if(!d.getElementById || !d.createTextNode) return;
    var ls = gebtn(d,'label');
    for (var i = 0; i < ls.length; i++) {
        var l = ls[i];
        if (l.className.indexOf('label_') == -1) continue;
        var inp = gebtn(l,'input')[0];
        if (l.className == 'label_check') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off';
            l.onclick = check_it;
        };
        if (l.className == 'label_radio') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
            l.onclick = turn_radio;
        };
    };
};
var check_it = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_check c_off' || (!safari && inp.checked)) {
        this.className = 'label_check c_on';
        if (safari) inp.checked = true;
    } else {
        this.className = 'label_check c_off';
        if (safari) inp.checked = false;
    };
};
var turn_radio = function() {
    var inp = gebtn(this,'input')[0];
    if (this.className == 'label_radio r_off' || inp.checked) {
        var ls = gebtn(this.parentNode,'label');
        for (var i = 0; i < ls.length; i++) {
            var l = ls[i];
            if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
        };
        this.className = 'label_radio r_on';
        if (safari) inp.checked = true;
    } else {
        this.className = 'label_radio r_off';
        if (safari) inp.checked = false;
    };
};
// Fancy checkboxes and radio bottons
///////////////////////////////////////////////////////////////////////////////////



function isEmail(value)
{
    return /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/i.test(value);
}

///////////////////////////////////////////////////////////////////////////////////
// news list
$(document).ready(function(){
    //$("#large_news").hide();

    $(".more").click(function () {
	  $(this).toggleClass('less');
	  return false;
    });
  });
// news list
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
// Content sec fade

var content_fader_timer;

$(document).ready(function() {
	$(".fade").fadeTo("slow", 0.22);

	$(".fade").click(function () {
		clearTimeout(content_fader_timer);
		$(this).fadeTo("slow", 1);
	});

	$(".fade input").bind('keydown', function () {
		clearTimeout(content_fader_timer);
		$(this).fadeTo("slow", 1);
	});

	$("div.fade").bind("mouseleave",function(){
    	content_fader_timer = setTimeout(function(){$("div.fade").fadeTo("slow", 0.22);}, 5000);
    });


});

// Content sec fade
///////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////
// general js util func start {
function callback_redirect(addr)
{
	location.href=addr;
}
// general js util func end }
//////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
// registracija start {
function submit_groupUpdate(el)
{
	var ix=el.selectedIndex;
	if(! (ix>=0 && ix < el.options.length))
	{
		return;
	}

	xajax_groupUpdate(el.options[ix].value);
}

function callback_groupUpdate(html)
{
	$('#group_container').html(html);
}


// registracija stop }
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
// terms and conditions start {
$(document).ready(function(){
	$("div.terms_box").hide();
    $(".terms").click(function () {
	  $("div.terms_box").slideToggle("fast");
	  return false;
    });
  });
// terms and conditions stop }
///////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////
// popup start {
function popMeUpScotty(url)
{
   w = window.open (url,
						  "bohunopopup",
						  "location=0,status=0,scrollbars=1,width=600,height=400,toolbar=0,menubar=0,directories=0");
	 //w.moveTo(0,0);

	 w.focus();
}
// popup stop }
///////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////
// mali popup start {
function toggleWrite()
{
	if( $('#popup_holder .popup_box').css('display') == 'none' )
	{
		$("#popup_holder .popup_box").addClass("show");
	}
	else
	{
		$("#popup_holder .popup_box").removeClass("show");
	}

	return false;
}

function closeWrite()
{
	$("#popup_holder .popup_box").removeClass("show");

}
// mali popup stop }
///////////////////////////////////////////////////////////////////////////////////

// postavlja vrijednost href atributa (drugi param) na linku ciji je id prosljeden u prvom parametru


///////////////////////////////////////////////////////////////////////////////////
// common functions {

function email_address_check(element_name) {
    var str = $('#' + element_name).val();

    if (str.length > 5)
    {
        $('#msg_1').show();

        if(isEmail(str))
        {
            $('#msg_1_text').attr('class', 'positive');
            $('#msg_1_text').text('Nice email address!');
        }
        else
        {
            $('#msg_1_text').attr('class', 'negative');
            $('#msg_1_text').text('Wrong email address!');
        }
    }
}

// } common functions
///////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////
// register {

function email_address_format_check(element)
{
    email_address_check('email');
}

function registration_email_address_format_check(element)
{
    email_address_check('bohuno_registration_email');
}

function password_length_check(element)
{
    var str = $('#bohuno_registration_password').val();

    $('#msg_2').show();
    if (str.length > 5)
    {
        $('#msg_2_text').attr('class', 'positive');
        $('#msg_2_text').text('Password is fine!');

        var str2=$('#bohuno_registration_password_repeated').val();
        if(str2 != '')
        {
            repeated_password_check( document.getElementById('bohuno_registration_password_repeated') );
        }
    }
    else if( str.length == 0)
    {
        $('#msg_2_text').attr('class', 'negative');
        $('#msg_2_text').text('Password is required.');
    }
    else
    {
        $('#msg_2_text').attr('class', 'negative');
        $('#msg_2_text').text('Password is too short.');
    }
}

function repeated_password_check(element)
{
	var str = $('#bohuno_registration_password_repeated').val();
	var pass_txt = $('#bohuno_registration_password').val();

	$('#msg_3').show();
    if (str == pass_txt)
    {
      $('#msg_3_text').attr('class', 'positive');
      $('#msg_3_text').text('Repeated password is fine!');
    }
    else
    {
      $('#msg_3_text').attr('class', 'negative');
      $('#msg_3_text').text('Repeated password is wrong!');
    }
}

function email_password_terms_check(element, emailFlag, passwordFlag, termsFlag)
{
    var return_val = true;

    if(emailFlag)
    {
        if ( !isEmail($('#bohuno_registration_email').val()) )
        {
            $('#msg_1').show();
            $('#msg_1_text').attr('class', 'negative');
            $('#msg_1_text').text('Valid email is required!');
            return_val = false;
        }
        else
        {
            $('#msg_1').hide();
        }
    }

    if(passwordFlag)
    {
        if ($('#bohuno_registration_password').val().length < 6)
       {
          $('#msg_2').show();
            $('#msg_2_text').attr('class', 'negative');
          $('#msg_2_text').text('Password is required.');
          return_val = false;
       }
       else
       {
         $('#msg_2').hide();
       }


        if ( $('#bohuno_registration_password').val().length >= 6 && ( $('#bohuno_registration_password_repeated').val().length < 6 || $('#bohuno_registration_password_repeated').val() != $('#bohuno_registration_password').val()  ) )
        {
          $('#msg_3').show();
          $('#msg_3_text').attr('class', 'negative');
          $('#msg_3_text').text('Password retype is required.');
          return_val = false;
       }
       else
       {
         $('#msg_3').hide();
       }
   }

    if(termsFlag)
    {
        if (! $('#terms:checked').length)
        {
            $('#msg_general_box').show();
            $('#msg_general_box').attr('class', 'negative');
            $('#msg_general_text').text('You must agree to terms and conditions!');
            return_val = false;
       }
       else
       {
           $('#msg_general_box').hide();
       }
    }

    return return_val;
}

function simple_email_password_terms_check(element, emailFlag, passwordFlag, termsFlag)
{
    var return_val = true;

    if(emailFlag)
    {
        if ( !isEmail($('#bohuno_registration_email').val()) )
        {
            return_val = false;
        }
    }

    if(passwordFlag)
    {
        if ($('#bohuno_registration_password').val().length < 6)
       {
          return_val = false;
       }


        if ( $('#bohuno_registration_password').val().length >= 6 && ( $('#bohuno_registration_password_repeated').val().length < 6 || $('#bohuno_registration_password_repeated').val() != $('#bohuno_registration_password').val()  ) )
        {
          return_val = false;
       }
   }

    if(termsFlag)
    {
        if (! $('#terms:checked').length)
        {
            return_val = false;
       }
    }

    return return_val;
}

// } register
///////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////
// contact {

function contact_email_address_format_check(element)
{
    email_address_check('contact_email');
}

function simple_email_contect_message_check(element)
{
    var return_val = true;

    if ( !isEmail($('#contact_email').val()) )
    {
        return_val = false;
    }

    return return_val;
}

// } contact
///////////////////////////////////////////////////////////////////////////////////



///////////////////////////////////////////////////////////////////////////////////
// naslovnica galerija/video tabovi
$(document).ready( function()
{
	$('#bohuno_registration_email').bind('keyup', function(el) {
		registration_email_address_format_check();
		if (simple_email_password_terms_check(el, true, true, true)) {
			//$('#register_submit[disabled]').fadeTo("slow", 1);
			if ($('#register_submit')) $('#register_submit').removeAttr("disabled");
		} else {
			//$('#register_submit[disabled!=disabled]').fadeTo("slow", 0.22);
			if ($('#register_submit')) $('#register_submit').attr("disabled", "disabled");
		}
	});

	$('#contact_email').bind('keyup', function(el) {
		contact_email_address_format_check();
        if (simple_email_contect_message_check(el)) {
        	$('#contact_submit[disabled]').fadeTo("fast", 1);
			$('#contact_submit').removeAttr("disabled");
		} else {
			$('#contact_submit[disabled!=disabled]').fadeTo("fast", 0.22);
			$('#contact_submit').attr("disabled", "disabled");
		}
	});


	$('#bohuno_registration_password').bind('keyup', function(el) {
		password_length_check();
		if (simple_email_password_terms_check(el, true, true, true)) {
			//$('#register_submit[disabled]').fadeTo("slow", 1);
			$('#register_submit').removeAttr("disabled");
		} else {
			//$('#register_submit[disabled!=disabled]').fadeTo("slow", 0.22);
			$('#register_submit').attr("disabled", "disabled");
		}
	});
	$('#bohuno_registration_password_repeated').bind('keyup', function(el) {
		repeated_password_check();
		if (simple_email_password_terms_check(el, true, true, true)) {
			//$('#register_submit[disabled]').fadeTo("slow", 1);
			$('#register_submit').removeAttr("disabled");
		} else {
			//$('#register_submit[disabled!=disabled]').fadeTo("slow", 0.22);
			$('#register_submit').attr("disabled", "disabled");
		}
	});

	$('#terms').bind('click', function(el) {
        if (simple_email_password_terms_check(el, true, true, true)) {
			//$('#register_submit[disabled]').fadeTo("slow", 1);
			$('#register_submit').removeAttr("disabled");
		} else {
			//$('#register_submit[disabled!=disabled]').fadeTo("slow", 0.22);
			$('#register_submit').attr("disabled", "disabled");
		}
	});

   	$('#bohuno_registration_email').bind('blur', registration_email_address_format_check);
	$('#bohuno_registration_password').bind('blur', password_length_check);
	$('#bohuno_registration_password_repeated').bind('blur', repeated_password_check);
	$('#contact_email').bind('blur', contact_email_address_format_check);

	$('#register_submit').bind('click', function (el) { return email_password_terms_check(el, true, true, true); } );
   	$('#newpassword_submit').bind('click', function (el) { return email_password_terms_check(el, true, true, false); } ); // promjena zaporke za zaboravne korisnike
   	$('#password_send').bind('click',  function (el) { return email_password_terms_check(el, false, true, false); } ); // settings
   	$('#contact_submit').bind('click',  function (el) { return simple_email_contect_message_check(el); } );

   	$('#contact_submit[disabled]').fadeTo("slow", 0.22);
   	//$('#register_submit[disabled]').fadeTo("slow", 0.22);

   	//alert($('#email').val().length);

   	/*
   	if (($('#email').length > 0) && ($('#bohuno_password').length > 0)) {
   		if (($('#email').val().length > 0) || ($('#bohuno_password').val().length > 0)) {
   			email_password_terms_check(0, true, true, false);
   			email_address_format_check(0);
   			password_length_check(0);
		}
	}
	*/

	// incijalizacija BohunoCountera za naslovnicu - prebaceno u home.html
	// BohunoCounter.init(); 

} );
// naslovnica galerija/video tabovi
///////////////////////////////////////////////////////////////////////////////////

function escapeHTML(str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}


function showHideBar(el)
{
	var obj=$('#info_top');
	var but=$(el);
	
	if(obj.hasClass('displaynone'))
	{
		obj.removeClass('displaynone');

		but.addClass('close');
		but.removeClass('open');
		
		$.cookie('hide_info_top', '', { expires: 365, path:'/'}); // set cookie	
	}
	else
	{
		obj.addClass('displaynone');
		
		but.addClass('open');
		but.removeClass('close');		
		
		$.cookie('hide_info_top', '1', { expires: 365, path:'/'}); // set cookie	
	}

}







/*
$(document).ready(function(){

    $(".open").click(function () {
	  $(this).toggleClass('close');
	  $('#info_top').toggle();
	  return false;
    });
  });
*/