$(document).ready(function(){

    /* multiple coupons are placed in div with couponContainer and autoHeight classes
     * to prevent being overflowed because of more than 1 code (their default 
     * height equals single code's height.
     */
    $(this)
    .find('.couponContainer.autoHeight')
    .attr('style','height:auto;');

    $('div.couponDesc').each(function(){
        $(this).after('<div class="hidden full">'+$(this).html()+'</div>');
        $(this).html($(this).html().substring(0,350) + '... ');
        $(this).append($(this).siblings('.links').find('.more'));
    });
    bindEvents();
});

function showFewMultipleCodes(couponer,howMany,speed){
    var allCodes = $(couponer).find('.code.multi');
    if (allCodes.length > howMany){
        var selectedCodes = $(allCodes).slice(0,howMany);
        if (selectedCodes != undefined){
            var text = $('#txtMoreCodes').text();
            var moreCodes = $('<a />',{
                'class' :'moreCodes',
                html: text
            });

            $(selectedCodes).last().after(moreCodes);
            showCodes($(selectedCodes),speed);

            moreCodes = $(couponer).find(('a.moreCodes'))
            $(moreCodes).unbind('click').click(function(event){
                event.stopPropagation();
                $(this).slideUp('fast',function(){
                    //show rest of the codes.
                    showCodes($(this).parent().find('.code.multi:hidden'),speed);
                });
            });
        }
    } //end if (allCodes.length > howMany).
    else{
        showCodes(allCodes,speed);
    }
}


function showCodes(codes,speed){
    $.each($(codes),function(i,code){
        setTimeout(function(){
            $(code).slideDown(speed)
        },1+(speed*i));
    });
}

function couponerClicked(couponer){
    var hiddenCodes = $(couponer).find('.codeHidden');
    $(hiddenCodes).fadeOut();

    var codes = $(couponer).find('.code');
    var multipleCodes = $(couponer).find('.code.multi');
    var onlySingleCodes = $(codes).filter(':not(.multi)');

    if (multipleCodes.length){
        showFewMultipleCodes(couponer,3,100);
    }
    else{
        $(codes).fadeIn();
    }

    $(multipleCodes).not(':first').attr('style','margin-top: 0');
    //$(onlySingleCodes).parents('dd').height($(onlySingleCodes).height()+2);

    var help = $(couponer).find('.couponHelp');
    
    if(help.length){
        $(help).slideDown(100);
    }

    window.open(webroot+'r/'+$(couponer).parents('.coupon').find('.couponId').html());

    $(couponer).unbind('click').click(function(){
        window.open(webroot+'r/'+$(couponer).parents('.coupon').find('.couponId').html());
    });

}

function bindEvents(){
    var couponers = $('#leftContent .couponer');

    $(couponers).unbind('click').click(function(){
        couponerClicked(this);
    });
    
    $(couponers).find('.singleCode').unbind('click').click(function(){

        codeClicked(this);
        return false;
    });
    $(couponers).find('a.couponLink').unbind('click').click(function(){
        window.open($(this).attr('href'));
        return false;
    });
    $(couponers).find('.user a').unbind('click').click(function(){
        window.location = $(this).attr('href');
        return false;
    });
    $('.couponDesc .more').bind('click', function(){
        var parentDiv = $(this).parent('div');
        $(parentDiv).html($(parentDiv).siblings('.full').html());
        return false;
    });

    var simple = $('.simple');

    $(simple).find('.simpleVoting:not(.outdated) .bad').click(function(){
        couponVoteSimple('Down', $(this));
    });
    $(simple).find('.simpleVoting:not(.outdated) .ok').click(function(){
        couponVoteSimple('Up', $(this));
    });  
}


function codeClicked(container){
    if (!container) return;
    var codes = $(container).find('.code');
    hiddenCodes = $(container).find('.codeHidden');
    $(hiddenCodes).fadeOut();
    $(codes).fadeIn();
    var multipleCodes = $(codes).filter('.multi');
    var onlySingleCodes = $(codes).filter(':not(.multi)');

    $(multipleCodes).attr('style','margin-top: 0').fadeIn();
    //$(onlySingleCodes).parents('dd').height($(onlySingleCodes).height()+2);
    window.open(webroot+'r/m/'+$(container).nextAll('span.couponMultiId').first().text());
}

function couponVoteSimple(type, current){

    couponId = current.siblings('.couponId').html();

    simpleVoting = current.parents('.simpleVoting');


    $(simpleVoting).children('.loading').show();
    if(couponId){
        $.get('/coupons/vote'+type, {
            id : couponId
        }, function(msg){
            if(msg == parseInt(msg)){
                current.find('span').html(msg);
            }
            $(simpleVoting).children('.loading').hide();
        });
    }
}

function sleep(milliseconds) {
    var start = new Date().getTime();
    while ((new Date().getTime() - start) < milliseconds){}
}

