var obj = null;
var BlockBackground = null;
var BlockContent = null;


$(document).ready(function() {

    $("a[rel='external']").click(function(){
        this.target = "_blank";
    });


    // ie6 png fix
    if ($.browser.msie && $.browser.version=="6.0") { DD_belatedPNG.fix('img, div, ul') }

    // backgroung sliding images/*
    $('#products').cycle({
        timeout:    5000,
        fx:         'fade',
        speed:      500,
        before:     function (curr,next,opts) { BlockBackground = $(next).attr('alt'); BlockContent = $(next).attr('id'); CloseBlock2(); $('#block').fadeOut("50");  },
        pagerAnchorBuilder: function(idx, slide) { return '<li><a href="#"></a></li>'; },
        pager:      '#pager'
    });

    $('#block').hover(function(){ $('#products').cycle('pause'); });
    
    $('div#img_layer').hover(function() {
        $('#products').cycle('pause');
    },
    function() {
        //$('#block').fadeOut("slow");
        $('#products').cycle('resume');
        //$('#block').load('slider/blank');
    });

    $('div#img_layer').click(function(){
        if ($('#block').css('display')=="none") {
            id = FindProductID(BlockContent);
            OpenProduct(id, '', 750);
        } else {
            $('#block').fadeOut("slow");
            $('#block').load('slider/blank');
        }
    })

    $("#left").click(function() {
        $(".slideshow").cycle('prev');
    });

    $("#right").click(function() {
        $(".slideshow").cycle('next');
    });

    $('.slideshow').cycle({
        fx:         'scrollLeft',
        timeout:    7000,
        pager:      '#nav',
        pause:      true
    });
});

function OpenProduct(id, tab, width) {
    $('#block').css({'width':width});
    $('#block').html('loading...');
    if (!tab) tab = 'zakladne';
    var product = FindProduct(id);
    $('#pager li:eq('+id+')').trigger('click');
    $('#products').cycle('pause');
    $("#block").css("background", BlockBackground);
    $.get('slider/'+BlockContent, function(data) {
        //$("#block").animate({width:sirka}, 1000, "bounceEaseOut", function() {  });
        $('#block').html(data);
        $.get('slider/'+product+'/'+tab, function(data) {
            $('#product_content').css({'display' : 'none'});
            $('#product_content').html(data);
            $('#product_content').fadeIn(500);
        });
        $('#block').fadeIn("slow");
    });
    
}

function FindProduct(id) {
    switch (id) {
        case 0 : return 'slimzone'; break;
        case 2 : return 'maxzone'; break;
        case 3 : return 'energy'; break;
        case 1 : return 'protein'; break;
        //case 4 : return 'lifeenergy'; break;
    }
}

function FindProductID(product) {
    switch (product) {
        case 'slimzone' : return 0; break;
        case 'maxzone' : return 2; break;
        case 'energy' : return 3; break;
        case 'protein' : return 1; break;
        //case 'lifeenergy' : return 4; break;
    }
}

function OpenPar(view) {
    //$('#block2').load('slider/'+view);
    //$('#block2').fadeIn("fast");
}

function CloseBlock2() {
    $('#block2').fadeOut("fast");
}
