function log(e){
    //return false;
    if (typeof(console) == 'object') 
        console.log(e);
    //else alert(e);
};

function AddFavorite(linkObj, addUrl3, addTitle3){
    if (document.all && !window.opera) {
        window.external.AddFavorite(addUrl3, addTitle3);
        return false;
    }
    else 
        if (window.opera && window.print) {
            linkObj.title = addTitle3;
            return true;
        }
        else 
            if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function')) {
                window.sidebar.addPanel(addTitle3, addUrl3, '');
                return false;
            }
    window.alert('Po potvrzení stiskněte CTRL-D,\nstránka bude přidána k Vašim oblíbeným odkazům.');
    return false;
}

var inputValue = {
    config: {
        rememberEl: '.rememberValue',
        values: new Array()
    },
    init: function(){
        $(inputValue.config.rememberEl).focus(function(){
            inputValue.name = $(this).attr('name');
            if (inputValue.config.values[inputValue.name] == null || $(this).val() == inputValue.config.values[inputValue.name]) {
                inputValue.config.values[inputValue.name] = $(this).val();
                $(this).val('').addClass('filled');
            }
        }), $(inputValue.config.rememberEl).blur(function(){
            inputValue.name = $(this).attr('name');
            if ($(this).val() == '') {
                $(this).val(inputValue.config.values[inputValue.name]).removeClass('filled');
            }
        })
    }
};

var externalLinks = {
    config: {
        externalClass: 'external',
        titleAppend: ' [odkaz na jiný web]'
    },
    init: function(){
        $('#main a[href^="http"]:not([href*=' + window.location.host + '])').each(function(){
            $(this).addClass(externalLinks.config.externalClass).attr('title', $(this).attr('title') + externalLinks.config.titleAppend);
        });
    }
};

var trackLinks = {
    init: function(){
        if (typeof(t) == 'object') {
            pageTracker = t;
        }
        if (typeof(pageTracker) == 'object') {
            $('a[href^="http"]:not([href*=' + window.location.host + '])').each(function(){
                $(this).click(function(){
                    pageTracker._trackPageview($(this).attr('href').replace(/(http|https):\/\//, '\/out\/'));
                });
            });
        }
    }
};

var sideLogin = {
    init: function(){
    
        this.box = $('#sideLogin');
        this.popup = $('#loginPopup');
        this.nlogged = this.box.find('.notLogged ');
        this.login = this.box.find('.login button');
        
        if (!this.isLogged() && !this.isError()) {
            this.popup.hide();
        }
        
        this.login.click(function(){
            sideLogin.popup.show();
            sideLogin.nlogged.hide();
            return false;
        });
    },
    isLogged: function(){
        return this.box.find('.loggedName').length;
    },
    isError: function(){
        return this.box.find('.warning').length;
    }
}

var bestSellers = function bestSellers(parentSelector){

    /**
     * Config
     */
    this.transitionDuration = 200;
    
    /**
     * Get elements
     */
    this.list = $(parentSelector).find('.list');
    this.items = this.list.children('.item');
    this.pictures = this.items.children('.picture');
    
    /**
     * Initialize method
     */
    this.init = function(){
        this.pictures.hide().eq(0).show();
        this.items.hover($.proxy(this, 'show'), null);
        this.list.height(this.list.height());
    }
    
    /**
     * Show method
     * @param {Object} ev
     */
    this.show = function(ev){
        var el = $(ev.currentTarget);
        if (!this.transition && !el.hasClass('opened')) {
            this.transition = true;
            this.pictures.filter(':visible').slideUp(this.transitionDuration).queue($.proxy(function(ev){
                this.transition = false;
                $(ev).dequeue();
            }, this));
            el.children('.picture').slideDown(this.transitionDuration).queue($.proxy(function(ev){
                this.transition = false;
                $(ev).dequeue();
            }, this));
            this.items.removeClass('opened');
            el.addClass('opened');
        }
    }
    
    
    this.transition = false;
    this.init();
};

/**
 * Products equal heights
 */
var products = {
    init: function(){
        // repeat self after load
        $(window).load(products.init);
        
        $('.products .odd').each(function(){
        
            ela = $(this).children('.inner');
            elb = $(this).next('.even').children('.inner');
            tita = ela.children('.title');
            titb = elb.children('.title');
            
            // TITLE
            var hta = tita.css('height', 'auto').height();
            var htb = titb.css('height', 'auto').height();
            if (hta < htb) {
                tita.height(htb);
                titb.height(htb);
            }
            else {
                tita.height(hta);
                titb.height(hta);
            }
            
            // INNER
            var ha = ela.css('height', 'auto').height();
            var hb = elb.css('height', 'auto').height();
            if (ha < hb) {
                ela.height(hb);
                elb.height(hb);
            }
            else {
                ela.height(ha);
                elb.height(ha);
            }
        });
    }
};

var order = {
	init: function()
	{
		var el = $('#deliveryAddress #delivery-same');
		if(el.length){
			order.deliveryAddress_check(el);
			el.change(function(){ order.deliveryAddress_check($(this)); });
		}
		$('.deliveryOption .description').hide();
		$('.deliveryOption .showDescription').click(function(){
			$(this).parents('.deliveryOption').find('.description').slideToggle();
		});
	}	,
	deliveryAddress_check: function(el)
	{
		if( $(el).is(':checked') )
		{
			$('#deliveryAddress .entries').hide();
			$('#deliveryAddress .entries input[type=text]').attr('value', '');
		}
		else
		{
			$('#deliveryAddress .entries').show();
		}
	}
}

/*******************************************
 *
 *  on DOM ready
 *
 ********************************************/
$(document).ready(function(){

    // Buttons transformations
    var shadows = ['#657c28', '#373232'];
    $('button,.button').filter(function(){
        return !$(this).parent('.buttonWrapper').length
    }).wrap('<span class="buttonWrapper"></span>').hover(function(){
        Cufon.replace($(this), {
            textShadow: '1px 1px 0 ' + shadows[1]
        });
    }, function(){
        Cufon.replace($(this), {
            textShadow: '1px 1px 0 ' + shadows[0]
        });
    }).focus(function(){ $(this).blur; }).parent().parent().addClass('cleaned');
    Cufon.replace('button,.button', {
        textShadow: '1px 1px 0 ' + shadows[0]
    });
    $('.buttonWrapper').filter(function(){
        return $(this).children().hasClass('addOverlay');
    }).children().append('<span class="overlay"><!-- --></span>');
    
    $('#page').prepend('<a href="#content" accesskey="0" class="forBlind">Přejít na obsah<' + '/a>');
    Cufon.replace('.cufon');
    inputValue.init();
    externalLinks.init();
    trackLinks.init();
    sideLogin.init();
    bs = new bestSellers('#bestSellers');
    
    $('a.lightbox,a[rel*=lightbox]').fancybox({
        titleShow: false
    });
    
	// PRODUCTS EQUAL HEIGHT
    products.init();
    
	// PLUS / MINUS
    $('.itemCount input.text').after('<a href="#" class="plus">+</a>').siblings('.plus').click(function(){
        $(this).siblings('input.text').attr('value', parseInt($(this).siblings('input.text').attr('value'))+1);
		return false;
    });
    $('.itemCount input.text').after('<a href="#" class="minus">-</a>').siblings('.minus').click(function(){
        $(this).siblings('input.text').attr('value', parseInt($(this).siblings('input.text').attr('value')) > 0 ? parseInt($(this).siblings('input.text').attr('value'))-1 : 0);
		return false;
    });
	
	// ORDER
	order.init();
	
    try {
        DD_belatedPNG.fix('.png,#spToolbar img,#cognito img');
    } 
    catch (e) {
    }
    
});

