/**
 * @author blazsak
 */
$(document).ready(function(){
    //if ( $.browser.msie && $.browser.version < 7 ) alert('old ie');
    
    
    $('#logo a, .pngfix').pngFix();
    //$(document).pngFix(); 
    //$("a.preview").lightbox();
    $("a.preview").fancybox({
        'overlayOpacity': 0.7,
        'overlayShow': true
    });
    //$("a.preview").fancybox(); 
    $("a.pagelink").fancybox({
        frameWidth: 800,
        frameHeight: 600
    });
    
    
    $("img[align='right']").css('float', 'right');
    $("img[align='left']").css('float', 'left');
    
    
    
    /*  
     
     
     $('.form-date').mask('9999-99-99');
     $('.form-kod').mask('9999-99-99');
     $('.form-int').mask('9999');
     
     
     $("#content-menu ul.content-menu").accordion({
     navigation: true,
     
     active: true,
     alwaysOpen: true,
     event: "mouseover"
     });
     
     $(".accordme").accordion({
     active: true,
     header: 'a.accordlink',
     navigation: true,
     alwaysOpen: false,
     autoHeight: true
     });
     
     
     $('#form-przypadek').submit(function(){
     $.floatbox({
     content: "<p style='margin:20px; padding: 20px; '>Trwa weryfikacja i przesyłanie formularza. <br/>Czas wysyłania zależy głównie od wielkości załączonych zdjęć.<br/>Prosimy o cierpliwość.<br/><img src='/gfx/admin/ajax-loader-small.gif' alt='...' /></p>",
     fade: true,
     button: ''
     
     });
     });
     
     
     $("#tabmenu").tabs({
     fx: {
     opacity: "toggle"
     }
     
     });
     */
    // SKLEP
    
    $('#sklep-kosz').ready(function(){
      if ($('#sklep-kosz').html()) {
        $('#sklep-kosz').html("<div class='ajax-loader'><img alt='...' src='/gfx/site/ajax-loader.gif' /></div>");
        //alert('init sklep');
        $.post("/ajax", {
          route: 'ajax',
          action: 'shopcart'
        }, function(data){
          //alert(data);
          //$("#sklep-kosz").hide();
          $("#sklep-kosz").html(data);
        // $("#sklep-kosz").show('slow');
        });
      }
    });
    
    $('.addtobasket').click(function(){
        var id = this.rel;
        var count = parseInt($('#prodcount-' + id).val());
        var debug;
        
        if (isNaN(count) || count < 1) {
            alert('Niepoprawna ilość')
            return false;
            
        }
        else {
            $('#sklep-kosz').html("<div class='ajax-loader'><img alt='...' src='/gfx/site/ajax-loader.gif' /></div>");
            $('.sklep-item-order').hide('fast');
            $.post("/ajax", {
                route: 'ajax',
                action: 'shopcart',
                id: id,
                table: 'add',
                count: count
            }, function(data){
            
                $("#sklep-kosz").html(data);
                $('.sklep-item-order').show('slow');
                
                
                
            });
            //alert(debug);
        }
        
        return false;
    });
    
    
    
    
    
    // ACTIVE FUNCTIONS
    
    $("a[rel='email']").each(function(){
        // Modify the mailto: value
        var mailtoVal = $(this).attr('href');
        mailtoVal = mailtoVal.replace("[email]", "mailto:");
        mailtoVal = mailtoVal.replace("[at]", "@");
        mailtoVal = mailtoVal.replace("[dot]", ".");
        // Auto-generate title tags for users
        var mailtoTitle = mailtoVal.replace("mailto:", "Email: ");
        $(this).attr('title', mailtoTitle);
        // onClick Event
        $(this).click(function(){
            window.location.href = mailtoVal;
            return false;
        });
    });
    
    
    $('.page h1, #kontaktform h3, ,h3.flashtitle').each(function(){
        var obj = $(this);
        var w = parseInt(obj.css('width'));
        //var h = parseInt(obj.css('height'))
        var tekst = obj.text();
        //alert(obj.css('width'));
        //if(!w) 
        w = 300;
        var flash = "<object type='application/x-shockwave-flash' data='/flash/title.swf' width='" + w + "' height='35'>";
        flash += "<param name='movie' value='/flash/title.swf' />";
        flash += "<param name='flashvars' value='napis=" + tekst + "' />";
        flash += "<param name='wmode' value='transparent' />";
        flash += "<param name='salign' value='TL' /></object> ";
        //  alert(flash);
        obj.html(flash);
        
    });
  
});

// PASSIVE FUNCTIONS  
function search_swap(option, text){
    var obj = $('#search_field');
    // alert(obj.val());
    if (option) {
        if (obj.val() == text) {
            obj.val('');
        }
    }
    else {
        if (obj.val() == '') {
            obj.val(text);
        }
    }
}

function deleteformbasket(id){
    //var id = href.rel;
    if (!id) {
        alert("Błąd funkcji koszyka\nSkontaktuj się z administratorem strony.");
        return false;
    }
    //var count = parseInt($('#prodcount-'+id).val());
    var debug;
    //alert('dlete init');
    
    $('#sklep-kosz').html("<div class='ajax-loader'><img alt='...' src='/gfx/site/ajax-loader.gif' /></div>");
    $('.sklep-item-order').hide('fast');
    $.post("/ajax", {
        route: 'ajax',
        action: 'shopcart',
        id: id,
        table: 'del'
    }, function(data){
    
        $("#sklep-kosz").html(data);
        $('.sklep-item-order').show('slow');
        
    });
    //alert(debug);
    
    return false;
    
}

