
var show_message = function(message_body){
    // Shows the message and hides it after 3 seconds
    $message_list = $('<ul id="messages"></ul>').css('display', 'none');

    $('<li></li>').text(message_body).appendTo($message_list);

    //$message_list.prependTo('body');
    var flash_message = $('#flash_message');
    $(flash_message).append($message_list);
    
    $message_list.slideDown(function(){
        var that = this;
        setTimeout(function(){
            $(that).slideUp();
        }, 5000);
    });
}


bind_menu = function(){
    /*
        Binds hover to items on the topbar menu (main navigation)
    */
    window.active_menu = undefined;
     
    var show_submenu = function(){
        var $el = $(this);
        $el.find('.submenu').slideDown('fast');
        $el.addClass('active');
        window.active_menu = $el;
    };
   
    var hide_submenu = function(){
        var $el = $(this);
        $el.find('.submenu').slideUp('fast');
        $el.removeClass('active');
        window.active_menu = undefined;
    };
    
    var show_submenu2 = function(){
        var $el = $(this);
        $el.find('.submenu').show();
    };

    var hide_submenu2 = function(){
        var $el = $(this);
        $el.find('.submenu').hide();
    };

    $('#menu').find('.toggle-submenu').hover(show_submenu, hide_submenu);
    //$('#photo_menu').hover(show_submenu2, hide_submenu2);
    $('#photo_menu').find('.submenu').show();
    
    
    
    /* Hover #photo_menu */
    
    function remove_px(value){
    	value = value.substring(0,(value.length -2));
    	return parseInt(value);
    };
    
    var size_main = 0;

    $('#photo_submenu.submenu').show();

    $("#photo_submenu.submenu > li").each(function(k,obj){
    	size_main= size_main + remove_px($(obj).css('height')) - 1; 
    });
    size_main = size_main + 'px';
    $("#photo_submenu.submenu").css('height', size_main);
    
    $("#photo_menu, #photo_submenu.submenu").hover(
        function(){
        	$("#photo_submenu.submenu").animate({height: size_main, display:'block'}, {queue:false});
        },
        function(){
        	$("#photo_submenu.submenu").animate({height: 0}, {queue:false});
        });
}

bind_bottom_menu = function(){
    /*
        Binds hover to items on the bottombar menu (social popups)
    */
    var $credits = $('#credits'),
        $subbtn = $credits.find('.toggle-submenu'),
        SUB_HEIGHT = 200;
        
    var news_hidden = false;
    var hide_news = function(){
        if(!news_hidden){
            news_hidden = true
            hide_submenu($('#news-widget'));
            $('#news-widget').hover(function(){
                show_submenu($(this));
            }, function(){
                hide_submenu($(this))
            });
            $('#close-news-widget').remove();
        }
    }

     var hide_submenu = function(el){
        el.find('.submenu').stop(true,true).animate({
            height: 0
        }, {queue:false, duration:300});
        el.removeClass('active');
    };

    var show_submenu = function(el){
        el.find('.submenu').stop(true,true).animate({
            height: el.attr('data-subheight') + 'px'
        }, {queue:false, duration:300});
        el.addClass('active');
    };
    
    $subbtn.hover(function(){
        hide_news();
        show_submenu($(this));
    }, function(){
        hide_submenu($(this))
    });

     $('#news-widget').find('.news-item:first').show();
    if(typeof homepage != 'undefined' && homepage){
        //show_submenu($('#news-widget'));
         $('#close-news-widget').click(function(){
            hide_submenu($('#news-widget'));
            $(this).remove();
        })
    }else{
        hide_news();
    }

}

var bind_fullscreen = function(){
    /*
        Binds the fullscreen action for the Full screen link on a product page
    */
    $container = $('#full-screen-container');
    var close_fullscreen = function(){
            $container.fadeOut();
    }
    var show_fullscreen = function(){
        $container.click(close_fullscreen).find('img').attr('src', this.href).
            end().fadeIn();
        return false;
    }

    $('#full-screen').click(show_fullscreen);
}

var bind_add_to_folio = function(){
    /*
        Binds the POST on the Add to Folio link on the product's detail page
    */
    $('#add-to-folio').click(function(){
        var dest = this.href;
        $.post(dest, function(data){
            if (data == 'OK') {
            	$('#add-to-folio').trigger('rem');
                show_message("The item was added to your custom folio");
            } else if (data == 'DUP') {
                show_message("The item was added to your custom folio");
            } else if (data == 'REM') {
            	$('#add-to-folio').trigger('add');
                show_message("The item was removed from your custom folio");
            } else if (data == 'AUTH') {
                // The user should log in
                window.location = dest;
                return true;
            } else {
                show_message("There was an error adding the item to your folio");
            }
        })
        return false;
    });
}

var bind_modal = function(){
    var $modal = $('#modal'),
        $canvas = $modal.find('.canvas'),
        $content = $modal.find('.content'),
        ESC_KEYCODE = 27,
        $window = $(window),
        $document = $(document);
    
    function hide_modal(){
        $modal.fadeOut(200, function(){
            $modal.addClass('hide');
        });
    }
    
    function show_modal(){
        $modal.removeClass('hide').fadeIn(200);
    }
    
    function close_on_esc(e){
        if (e.keyCode == ESC_KEYCODE) {
            e.preventDefault();
            $document.unbind('keydown', close_on_esc);
            hide_modal();
        }
    }
    
    function load_modal(e){
        e.preventDefault();
        e.stopPropagation();
        var url = this.href;
        $content.html('').addClass('wait')
            .load(url, function(){
                $content.removeClass('wait');
                $content.find('a').click(load_modal);
            });
        show_modal();
        $document.bind('keydown', close_on_esc);
        return false;
    }
    
    $modal.find('a.close').click(hide_modal);
    
    $('#credits').find('a.modal').click(load_modal);
}

var bind_modal_back = function(){
    if(typeof IS_MODAL != 'undefined' && IS_MODAL){

        $('.modal-back').show();
/*
        $(window).resize(function(){
           adjust_modal_back();
        });
        $(window).scroll(function(){
           adjust_modal_back();
        });

        var adjust_modal_back = function(){
            $('.modal-back').css({
               width: $(document).attr('width'),
               height: $(document).height()
           })
        }
*/
    }
}

var news_next = function(){
    var items = $('.news .news-item');
    if(items.length > 0){
        var current = $('.news .news-item:visible');
        if(current.next().length){
            current.next().show();
        }else{
            $(items[0]).show();
        }
        current.hide();
    }
}

function show_submenu(el){
	el.find('.submenu').stop(true,true).animate({
		height: el.attr('data-subheight') + 'px'
		}, {queue:false, duration:300});
	el.addClass('active');
};	


$(function(){
    bind_modal_back();
    bind_menu();
    bind_fullscreen();
    bind_add_to_folio();
    bind_bottom_menu();
    bind_modal();
});
