var MENU_TAB_ID = '';

toggleList = function(tree_id) {
  $("#parent"+tree_id).slideToggle("medium");
}

localizeSet = function(sel){
  var v = $("#"+sel+"-select option:selected").val();
  $('#'+sel+'-form').submit();
}

chat = function(){
  window.open('http://chat.livechatinc.net/licence/1030226/open_chat.cgi?lang=en'+'&amp;groups=0&amp;dc='+escape(document.cookie+';l='+document.location+';r='+document.referer+';s='+typeof lc_session),'Czat_1030226','width=529,height=520,resizable=yes,scrollbars=no,status=1');
}

var localizeOption = '';
function createDropDown(id){
  var source = $("#"+id+"-select");
  source.hide();
  var selected = source.find("option[selected]");
  var options = $("option", source);
  
  $("#"+id).append('<dl id="'+id+'-target" class="localization"></dl>')
  
  if ( id == 'language-choice') {
    $("#"+id+"-target").append('<dt><a href="javascript:;">' +
        '<img src="/framework/images/flags/'+selected.val()+'.gif" />' +
        selected.text() + 
        '<span class="value">' + selected.val() + 
        '</span></a></dt>')
    $("#"+id+"-target").append('<dd><ul></ul></dd>')
    options.each(function(){
        $("#"+id+"-target dd ul").append('<li><a href="javascript:;">' +
            '<img src="/framework/images/flags/'+$(this).val()+'.gif" />' +
            $(this).text() +
            '<span class="value">' + 
            $(this).val() + '</span></a></li>');
    });
  } else {
    $("#"+id+"-target").append('<dt><a href="javascript:;">' +
        selected.text() + 
        '<span class="value">' + selected.val() + 
        '</span></a></dt>')
    $("#"+id+"-target").append('<dd><ul></ul></dd>')
    options.each(function(){
        $("#"+id+"-target dd ul").append('<li><a href="javascript:;">' +
            $(this).text() +
            '<span class="value">' + 
            $(this).val() + '</span></a></li>');
    });
  }
}

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(!$.support.opacity)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeTo = function(speed,to,callback) {
		return this.animate({opacity: to}, speed, function() {
			if (to == 1 && jQuery.browser.msie)
				this.style.removeAttribute('filter');
			if (jQuery.isFunction(callback))
				callback();
		});
	};
})(jQuery);

$(document).ready(function(){
  
  
  
  // beta
  $('.select-pretty-selected.currency').bind('click', function(){
    $('#currency-select').slideToggle('fast');
  });
  $('.select-pretty-selected.language').bind('click', function(){
    $('#language-select').slideToggle('fast');
  });

  $(document).bind('click', function(e) {
      if ( $('#currency-select').is(':visible') || $('#language-select').is(':visible') ) {
        $('#currency-select').hide();
        $('#language-select').hide();
      }
  });
  
  
  
  
  createDropDown('language-choice');
  createDropDown('currency-choice');
  
  $(".localization dt a").click(function() {
      localizeOption = $(this).parents().parents().parents().attr('id');
      $(".localization dd ul").hide();
      $("#"+localizeOption+"-target.localization dd ul").toggle();
  });
  $(document).bind('click', function(e) {
      var $clicked = $(e.target);
      if ( localizeOption && !$clicked.parents().hasClass("localization"))
          $("#"+localizeOption+" .localization dd ul").hide();
  });
  
  $(".localization dd ul li a").click(function() {
      var text = $(this).html();
      $("#"+localizeOption+" .localization dt a").html(text);
      $("#"+localizeOption+" .localization dd ul").hide();
      
      var source = $("#"+localizeOption+"-select");
      source.val($(this).find("span.value").html())
      localizeSet(localizeOption);
  });
  
  
  
  $('input.text').bind({
    'focus' : function(){
      if (this.value == this.defaultValue){
        	this.value = '';
    	}
        if(this.value != this.defaultValue){
	    	this.select();
        }
    },
    'blur' : function(){
      if (this.value == ''){
        	this.value = (this.defaultValue ? this.defaultValue : '');
    	}
    }
  });
  
  
  
  $("#cart-choice li a").click(function() {
      $(this).blur();
      $('#cart-choice-inner').customFadeTo("fast", 0.23);
      
      var req = "cartChoice="+$(this).attr('rel')+"&mode=ajax&rand="+(new Date()).getTime();
      
      $('#shopping-cart').addClass('cart-loader');
      
      $('#cart-choice li a').removeClass('active');
      $(this).addClass('active');
      
      $.ajax({
      url    : "/cgi-bin/index.pl",
      type   : "POST",
      data   : req,
      success: function(r){
        $('#shopping-cart').removeClass('cart-loader');
        $('#cart-choice-inner').customFadeTo("fast", 1).html(r);
      },
      error: function(){ location.reload(); }
      });
      
      
      return false;
  });
  
  
    $("#boxLoginForm").submit(function(){
        if($("#box_authorize_login").val().length==0)
        {
            alert("Enter your username!");
            $("#box_authorize_login").focus();
            return false;
        }
        if(!/^[a-zA-Z0-9\._-]+$/.test($("#box_authorize_login").val()))
        {
            alert("You have entered an incorrect user name. Make sure that you correctly typed your username!");
            $("#box_authorize_login").focus();
            return false;
        }
        if($("#box_authorize_password").val().length==0)
        {
            alert("Enter the password that you used to log in to your account on the site!");
            $("#box_authorize_password").focus();
            return false;
        }
        return true;
    });
    
    $("#loginForm").submit(function(){
        if($("#authorize_login").val().length==0)
        {
            alert("Enter your username!");
            $("#authorize_login").focus();
            return false;
        }
        if(!/^[a-zA-Z0-9\._-]+$/.test($("#authorize_login").val()))
        {
            alert("You have entered an incorrect user name. Make sure that you correctly typed your username!");
            $("#authorize_login").focus();
            return false;
        }
        if($("#authorize_password").val().length==0)
        {
            alert("Enter the password that you used to log in to your account on the site!");
            $("#authorize_password").focus();
            return false;
        }
        return true;
    });
    
    
    $("#boxResetPasswordForm").submit(function(){
        if($("#box_reset_login").val().length==0 && $("#box_reset_email").val().length==0)
        {
            alert("Enter your username or e-mail address you used when registering!");
            $("#box_reset_login").focus();
            return false;
        }
        return true;
    });
    
	$("#boxSearchForm").submit(function(){
        if($("#box_qs").val().length==0)
        {
            alert("Enter the name of the product you are looking for!");
            $("#box_qs").focus();
            return false;
        }
        return true;
    });
	
    $("#language-choice-select").bind('change', function(){
      localizeSet('language-choice');
    });
    
	
	/*
    $("#lostPasswordLink").click(function(){
        $("#loginPanelBox").fadeOut("medium", function(){
            $("#lostPasswordPanelBox").fadeIn("medium", function(){
                $("#box_reset_login").focus();
            });
        });
        return false;
    });
    $("#lostPasswordBackLogin").click(function(){
        $("#lostPasswordPanelBox").fadeOut("medium", function(){
            $("#loginPanelBox").fadeIn("medium", function(){
                $("#box_authorize_login").focus();
            });
        });
        return false;
    });
    
    $("#mailThisPageForm").submit(function(){
        if (!/^[a-zA-Z0-9\-_\.]+?@[a-zA-Z0-9\-_\.]+?\.[a-zA-Z]{2,4}$/.test($("#mail_this_od").val()))
        {
            alert("Podaj swój adres e-mail!");
            $("#mail_this_od").focus();
            return false;
        }
        if (!/^[a-zA-Z0-9\-_\.]+?@[a-zA-Z0-9\-_\.]+?\.[a-zA-Z]{2,4}$/.test($("#mail_this_do").val()))
        {
            alert("Podaj adres e-mail osoby, do której chcesz przesłać wiadomość!");
            $("#mail_this_do").focus();
            return false;
        }
        return true;
    });
    
    $("#commentForm").submit(function(){
       if($("#comment").val().length==0 || $("#comment").val()=="Tutaj wpisz swój komentarz...")
        {
            alert("Wpisz treść komentarza, który chcesz dodać!");
            $("#comment").focus();
            return false;
        }
       return true; 
    });*/
    
    
    $('#linkToLoginBox').click(function(){
        $.scrollTo('#boxLoginForm', 800, {onAfter:function(){
            $('#box_authorize_login').focus();
        }});
        return false;
    });
    
    
    $('ul#cardMenu li a').click(function()
    {
        if(MENU_TAB_ID==""){
            $('ul#cardMenu li').each(function(){
                if($(this).hasClass('selected')){
                    MENU_TAB_ID=$(this).attr("id");
                }
            });
        }
        var id = $(this).parent().attr("id");
        if (MENU_TAB_ID!=id)
        {
            $('#'+id+'_content').show();
            $('#'+MENU_TAB_ID+'_content').hide();
            
            $('#'+id).addClass('selected');
            $('#'+MENU_TAB_ID).removeClass('selected');
            
            MENU_TAB_ID=id;
        }
        return false;
    });
    
    
    $('.cartDropItem').click(function(){
        if(confirm("Delete this product from cart?"))
        {
            return true;
        }
        return false;
    });
    
    
    
    
    $('a[rel*=lightbox]').fancybox();
    
    
	$('#StronaDrukuj').click(function()
    {
        window.open($(this).attr("href"), "printWindow", "width=760,height=680,toolbar=0,locationbar=1,directories=0,status=1,menubar=0,scrollbars=1,resizable=1,copyhistory=0");
        return false;
    });
    
    $('a.productLists').click(function()
    {
        $.cookie('ProductListsViewType', $(this).attr("rel"), { expires: 31, path: "/" });
        location.reload(true);
        return false;
    });
	
    
	$(".newsletterForm").submit(function(){
        var id = (this.name=="box") ? "#box_" : "#";
        if($(id+"newsletter_imie").val().length==0)
        {
            alert("Enter your first name!");
            $(id+"newsletter_imie").focus();
            return false;
        }
        if (!/^[a-zA-Z0-9\-_\.]+?@[a-zA-Z0-9\-_\.]+?\.[a-zA-Z]{2,4}$/.test($(id+"newsletter_email").val()))
        {
            alert("Please enter a valid email address!");
            $(id+"newsletter_email").focus();
            return false;
        }
        return true;
    });
    
    
    
    
    $('*[title]').qtip({
      style: {
        name: 'dark',
        tip: true,
        border: {
            width: 1,
            radius: 5,
            color: '#c8c8c8'
        },
        padding: 4,
        color: '#313131',
        background: '#ebebeb',
        fontSize: '11px',
        fontFamily: 'Tahoma,Arial',
        lineHeight: '14px',
        textAlign: 'justify'
      },
      show: {
        solo: true,
        delay: 0
      },
      position: {
        adjust: {
          screen: true
        }
      }
    });
    
});
