/*
    Title:  Confessions of a tshirt
    Description:
        JavaScript for the confessions of a tshirt site
    
    URL: http://confesionsofatshirt.com
    Version: 0.1
*/

var confess = {
    // Variables
    display_tshirt :    '#display_tshirt',
    tshirt_list :       '#tshirt_list',
    tshirt_class :      '.tshirt',
    twitter_class :     '.tweet',
    facebook_class :    '.facebook',
    rate_class :        '.rate',
    select_class:       'select',
    
    
    init : function() {
        this.addEvents();
    },
    
    addEvents : function() {
        // Add click event to each tshirt image
        $(this.tshirt_class).each(function(index, item) {
            $(item).bind('click', confess.swapImage);
        });
        
        // Add share links
        $(this.tshirt_list + ' LI').each(function(index, item) {
           //$(item).bind('click', confess.swapImage);
           var slogan = $(item).children('h2').text();
           
           confess.tweetIt($(item).find('a' + confess.twitter_class), slogan);
        });
        
        $(this.facebook_class).each(function(index, item) {
            $(item).bind('click',confess.facebookIt);
        });
        
        $('#buy LABEL').bind('click', confess.changeLabel);
        
        $('#buy INPUT[type=radio]').bind('click', confess.changeLabel);
        
    },
    
    swapImage : function(e) {
        if ($('#buy').css('display') == 'none') {
            $('#buy').show();
        }
        
        // Variables
        var small_suffix = '_small.png';
    
        // Prevent following the link
        e.preventDefault();
        
        // Build small display tshirt URL
        var img = $(this).find('img');
        var img_src = img.attr('src');
        var small_src = img_src.replace(/.png/, small_suffix);
        
        // Set new Display t-shirt src URL
        $(confess.display_tshirt).css('background-image', 'url('+small_src+')');
        
        // Update Form values
        var product_id = $(this).attr('id');
        $('#buy input[type=radio]').each(function(index, item) {
            $(this).attr('name', 'size_' + product_id);
        });

        
        $('#form_color').attr('name', 'color_' + product_id);
        $('#form_qty').attr('name', 'qty_' + product_id);


    },
    
    changeLabel : function(target) {
        // Remove and selected class
         $('#buy LABEL').each(function(index, item) {
            $(item).removeClass(confess.select_class);
        });
        
        // If radio button clicked
        if ($(this).attr('type') == 'radio') {
            $(this).siblings('label').addClass(confess.select_class);
        } else {
            $(this).addClass(confess.select_class);
        }
        
    },
    
    rateTee : function() {
    
    },
    
    buyIt : function() {
    
    },
    
    tweetIt : function(item, slogan) {
        var twitter_url = 'http://twitter.com/home?status=%22';
        var end_msg = '" #confesstee http://bit.ly/ctees'
        
        // Check size of slogan
        if (slogan.length >= '103') {
            slogan = slogan.slice(0,103) + '...';
        }
        
        var final_url = twitter_url + encodeURIComponent(slogan + end_msg);
        
        // Set URL
        $(item).attr('href', final_url);
        
        // set click actions
        $(item).bind('click', function(e) {
            e.preventDefault();
            window.open($(this).attr('href'), 'Share on Twitter', 'resizable=1,width=826,height=436');
        });
        
    },
    
    facebookIt : function(e) {
        e.preventDefault();
        
        var fb_url = 'http://www.facebook.com/sharer.php?src=confess';
        fb_url += '&u=' + encodeURIComponent(document.location);
        fb_url += '&t=' + encodeURIComponent(document.title);
        
        window.open(fb_url, 'Share on Facebook', 'resizable=1,width=626,height=436');
     }

}

// Run on when the document it ready
$(document).ready(function() {
    confess.init();
});
