//function showSpinner(event)
//{
//  $("#ajax-spinner").show().css({
//    position: "absolute",
//    left: event.pageX + 20,
//    top: event.pageY + 40
//  });
//}

function showSpinner()
{
  $("#loader").show();
}

$(document).ready(function(){
    
    //CLOSE
    
    $("#fbBarOffersClose").click(function(){
      var u = $(this).attr('rel');
      $.get(u);
      $("#facebook").slideUp(500);
    });
    
    $("#where .box .close").click(function(){
      $("#where").fadeOut(300);
    });
    
    
    // share
    $("#share_bg .center .box .close").click(function(){
      $("#share_bg").fadeOut(300);
    });
    $(".showShare").live('click', function() {
      $("#share_bg").fadeIn(300);
      $('#recAddr').val($(this).attr('rel'));
      $('#recFb').attr('href', 'http://www.facebook.com/sharer.php?u='
                + encodeURIComponent($(this).attr('rel')));
                //+ '&t=' + encodeURIComponent('I really like this deal! Check it out:'));
      $('#recTweet').attr('href', 'http://twitter.com/share?url='
                + encodeURIComponent($(this).attr('rel'))
                + '&text=' + encodeURIComponent($(this).attr('nameOffer')+' @'+$(this).attr('tw')));
      $('#frmshareMail-idOffer').val($(this).attr('idOffer'));
    });
    
    //THANX
//    $("#right .bluebox.news input.text").click(function(){
//      $("#thanx").fadeIn(300);
//    });
    $("#thanx .box .close").click(function(){
      $("#thanx").fadeOut(300);
    });
    
    //RIGHT SKRYVANI
    $("#right h2.nadpis.n1").toggle(function(){
      $(this).removeClass("active");
      $("#right .blok.b1").slideUp(500);
    }, function(){
      $(this).addClass("active");
      $("#right .blok.b1").slideDown(500);
    });
    $("#right h2.nadpis.n2").toggle(function(){
      $(this).removeClass("active");
      $("#right .blok.b2").slideUp(500);
    }, function(){
      $(this).addClass("active");
      $("#right .blok.b2").slideDown(500);
    });
    $("#right h2.nadpis.n3").toggle(function(){
      $(this).addClass("active");
      $("#right .blok.b3").slideDown(500);
    }, function(){
      $(this).removeClass("active");
      $("#right .blok.b3").slideUp(500);
    });
    $("#right h2.nadpis.n4").toggle(function(){
      $(this).addClass("active");
      $("#right .blok.b4").slideDown(500);
    }, function(){
      $(this).removeClass("active");
      $("#right .blok.b4").slideUp(500);
    });
    
    //CHOOSE
    $("#top .choose .tlacitko").toggle(function(){
      $("#top .choose .blok").slideDown(500);
    }, function(){
      $("#top .choose .blok").slideUp(500);
    });
    
    //CENA - POSUVNIKY
    var logPrice = function(sliderValue, min, max)
    {
      if (sliderValue == 0) return min;
      if (sliderValue == 300) return max;
//      var k = max/(5.736-Math.log(8));
//      var p = 5.736-Math.log(308-sliderValue);
      var k = max/Math.pow(0.001, -0.9);
      var p = Math.pow(0.001, -0.003*sliderValue);
      return Math.round(k*p);
    }
   
    if (typeof maxPrice=="undefined") maxPrice = 1000;
    if (typeof currency_symbol=="undefined") currency_symbol = '';
    $( "#slider-range-price" ).slider({
			range: true,
			min: 0,
			max: 300,
			values: [ 0, 300 ],
			slide: function( event, ui ) {
				$( "#right .blok.b1 .cena .left" ).text( currency_symbol + ' ' + logPrice(ui.values[0], 0, maxPrice) );
				$( "#right .blok.b1 .cena .right" ).text( currency_symbol + ' ' + logPrice(ui.values[1], 0, maxPrice) );
        
        $('#priceStart').val(logPrice(ui.values[0], 0, maxPrice));
        $('#priceEnd').val(logPrice(ui.values[1], 0, maxPrice));
			},
      stop: function(event, ui) {
        showSpinner();
        $('#filterForm').ajaxSubmit();
      }
		});
		$( "#right .blok.b1 .cena .left" ).text( currency_symbol + ' ' + logPrice(0, 0, maxPrice) );
		$( "#right .blok.b1 .cena .right" ).text(  currency_symbol + ' ' + logPrice(300, 0, maxPrice) );
    
    //DISCOUNT - POSUVNIKY
    $( "#slider-range-discount" ).slider({
			range: true,
			min: 0,
			max: 100,
			values: [ 0, 100 ],
			slide: function( event, ui ) {
				$( "#right .blok.b3 .discount .left" ).text( ui.values[ 0 ] + ' %' );
				$( "#right .blok.b3 .discount .right" ).text( ui.values[ 1 ] + ' %' );
        
        $('#discountStart').val(ui.values[ 0 ]);
        $('#discountEnd').val(ui.values[ 1 ]);
			},
      stop: function(event, ui) {
        showSpinner();
        $('#filterForm').ajaxSubmit();
      }
		});
		$( "#right .blok.b3 .discount .left" ).text( $( "#slider-range-discount" ).slider( "values", 0 ) + ' %' );
		$( "#right .blok.b3 .discount .right" ).text(  $( "#slider-range-discount" ).slider( "values", 1 ) + ' %' );



    $('#loader').ajaxStop(function () {
        $('#loader').hide();
    });
    
    $('#searchSubmit').click(function() {
      $('#searchTextForm').val($('#searchText').val());
      showSpinner();
      $('#filterForm').ajaxSubmit();
    });
    $('#searchText').keypress(function() {
      if (event.which == 13)
      {
        $('#searchTextForm').val($('#searchText').val());
        showSpinner();
        $('#filterForm').ajaxSubmit();
      }
    });
    
    $('.categories_input').click(function() {
      showSpinner();
      $('#useCategories').val(1);
      $('#filterForm').ajaxSubmit();
    });
    
    // paging
    $('.paging').live('click', function() {
      $('#page').val($(this).attr('rel'));
      $('#filterForm').ajaxSubmit();
      showSpinner();
      $('#page').val(1);
    });
    
    // search
    $('.tag_search').click(function() {
      $('#searchTextForm').val($(this).attr('rel'));
      $('#searchText').val($(this).attr('rel'));
      $('#filterForm').ajaxSubmit();
      showSpinner();
    });
    
    // ajaxove odeslani mailu
//    $('#frmaddMail-add').click(function() {
//      showSpinner();
//      $.post( $('#frm-addMail').attr('action'), {mail: $('#frmaddMail-mail').val()},
//      function(data){
//         $('#sent_email').css('display', 'block');
//         $("#thanx").fadeIn(300);
//       });
//      return false;
//    });
    
    //$(".selectbox").selectbox();
    
    //MALE/FEMALE
    $("#left #gender .male").click(function(){
      $(this).addClass("active");
      $("#left #gender .female").removeClass("active");
      $('.newsletter_mf').val('male');
      
    });
    $("#left #gender .female").click(function(){
      $(this).addClass("active");
      $("#left #gender .male").removeClass("active");
      $('.newsletter_mf').val('female');
    });
    
    //SHARE
    $("#share_bg .center .box .ico.mail").click(function(){
      $("#share_bg .center .box .ico.mail").css("background-position","0 -90px");
      $("#share_bg .center #share_mail").css("display","block");
      $("#share_bg .center .box .ico.adresa").css("background-position","0 0");
      $("#share_bg .center #share_adresa").css("display","none");
    });
    $("#share_bg .center .box .ico.adresa").click(function(){
      $("#share_bg .center .box .ico.adresa").css("background-position","0 -90px");
      $("#share_bg .center #share_adresa").css("display","block");
      $("#share_bg .center .box .ico.mail").css("background-position","0 0");
      $("#share_bg .center #share_mail").css("display","none");
    });
    
    //SIGN IN - INPUTY
    $("#sign_up span.first input").click(function(){
      if(this.value==this.defaultValue)this.value="";
    }); 
    $("#sign_up span.last input").click(function(){
      if(this.value==this.defaultValue)this.value="";
    });
    $("#sign_email").click(function(){
      if(this.value==this.defaultValue)this.value="@";
    });
//    $("#sign_up span.password input").click(function(){
//      if(this.value=="Password"){
//        this.value="";
//      }
//    });
//    $("#sign_up span.confirm input").click(function(){
//      if(this.value=="Confirm password"){
//        this.value="";
//      }
//    });
    
//    $('#sign_create').click(function() {
//      var ok = true;
//      if($('#sign_pass_1').val() != $('#sign_pass_2').val())
//      {
//        ok = false;
//        $('#sign_pass_2').addClass('error');
//      }
//      return ok;
//    });
    
    $('#r_mail').click(function() {
      $("#share_bg").fadeIn(300);
      $("#share_bg .center .box .ico.mail").css("background-position","0 -90px");
      $("#share_bg .center #share_mail").css("display","block");
      $("#share_bg .center .box .ico.adresa").css("background-position","0 0");
      $("#share_bg .center #share_adresa").css("display","none");
      $('#frmshareMail-idOffer').val($(this).attr('idOffer'));
    });
    
    $('#r_twitter').click(function() {
      $("#share_bg").fadeIn(300);
    });
    $('#r_fb').click(function() {
      $("#share_bg").fadeIn(300);
    });
    $('#r_link').click(function() {
      $("#share_bg").fadeIn(300);
      $('#recAddr').val($(this).attr('rel'));
      $("#share_bg .center .box .ico.adresa").css("background-position","0 -90px");
      $("#share_bg .center #share_adresa").css("display","block");
      $("#share_bg .center .box .ico.mail").css("background-position","0 0");
      $("#share_bg .center #share_mail").css("display","none");
    });
    
    //STRANKOVANI - POSUN
    $('#strankovani_hp li a').live('click', function() {
      $('html, body').animate({scrollTop: '286px'}, 700);
    });
    
    
    // SELECTBOX
    $(".selectbox").selectbox();
    
    // right filter, check
    $('.cat_only').click(function() {
      $('.cat_check_in').removeAttr("checked");
      $('#check_'+$(this).attr('rel')).attr("checked", "checked");
      $('#useCategories').val(1);
      $('#filterForm').ajaxSubmit();
      showSpinner();
    });
    $('.source_only').click(function() {
      $('.sources_check_in').removeAttr("checked");
      $('#check_s_'+$(this).attr('rel')).attr("checked", "checked");
      $('#filterForm').ajaxSubmit();
      showSpinner();
    });
    $('.sources_check_in').click(function() {
      showSpinner();
      $('#filterForm').ajaxSubmit();
      showSpinner();
    });
    
    
    $('input[name|="password"]').focus(function() {
      if($(this).val() == this.defaultValue)
      {
        $(this).val('');
      }
      this.type="password";
    });
    $('input[name|="cPassword"]').focus(function() {
      if($(this).val() == this.defaultValue)
      {
        $(this).val('');
      }
      this.type="password";
    });
    
    //(DE)ACTIVATE
    $("#left #profile1 .newsletter .prepinac").toggle(function(){
      $(this).addClass("noactive"); 
    }, function(){
      $(this).removeClass("noactive");
    });
    
    //MALE/FEMALE
    $("#left #profile2 .bluebox .male").click(function(){
      $(this).addClass("active");
      $('.newsletter_mf').val('male');
      $("#left #profile2 .bluebox .female").removeClass("active");
      
    });
    $("#left #profile2 .bluebox .female").click(function(){
      $(this).addClass("active");
      $('.newsletter_mf').val('female');
      $("#left #profile2 .bluebox .male").removeClass("active");
    });
    
    //FILE INPUT
    $('#left #profile2 .bluebox.foto .right span.upload').mouseover(function(){
		  $(this).addClass('hover');
		}).mouseout(function(){
		  $(this).removeClass('hover');
		});
//		$('#left #profile2 .bluebox.foto .right span.upload').file().choose(function(e, input){
//		  $('#left #profile2 .bluebox.foto .right span.orange').text(input.val());
//		});

    //LANG
    $("#top .lang .tlacitko").click(function(){
      if($("#top .lang .blok").css('display')=='none')
        $("#top .lang .blok").slideDown(500);
      else
        $("#top .lang .blok").slideUp(500);
    });
    $("#top .lang .blok a").click(function(){
      $("#top .lang .tlacitko span").text($(this).attr('class'));
      $("#top .lang .blok").slideUp(500);
    });

});

function validateEmail(email) 
{ 
 var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ 
 return re.test(email) 
}


