$(document).ready(function(){
    
    $('#cashback').live('click', function(){
        $.getCashbackStatus(false);
        return false;
    });
    
    $('#cashback-close').live('click', function(){
        popup_close(popup_counter);
        return false;
    });

    $.getCashbackStatus = function(automatic){
        var cashbackUrl = "/popup_cashbackpromo/index.php";
        if (automatic) {
            cashbackUrl = "/popup_cashbackpromo/index.php?auto=1";
        }
        $.ajax({
            url: cashbackUrl,
            success: function(result){
                if(result != 'false' && result != '' && result != 'null') {
                    $.showCashbackStatus(result);
                }
            }
        });
    }
    
    // show cashbackpopup on start
    if ($('#assortment').length > 0) {
        $.getCashbackStatus(true);
    }

    $.showCashbackStatus = function(result){
		if ($(result).find('#popup_cashbackpromo').length < 1) {
			return false;
		}
        popup_counter++;
        popup_width = 440;
        popup_height = 500;

        // local vars
        var page_width = 990;
        var page_height = 560;

        // calculate position
        var left = page_width/2 - popup_width/2;
        var top = page_height/2 - popup_height/2;

        if( top < 0 ) top = 0;
        
        var c = '<div style="left:'+left+'px; top:'+top+'px; width:'+popup_width+'px; height:'+popup_height+'px; z-index:'+popup_counter+';" class="popup" id="popup_'+popup_counter+'">';
        c += '<div class="popup_closelink"><a href="javascript:popup_close('+popup_counter+');"><img src="/.file/popup_close.gif" /></a></div>';
        c += '<div class="popup_content" id="popup_'+popup_counter+'_content">' + $(result).find('#popup_cashbackpromo').html() + '</div>';
        c += '</div>';
        
        $("#wrapper").append( c );
    }
});


