
/**
 * Greenhome User Interface
 */ 
var GREENHOME = {

    url: "www.greenhome.cz/",

    /**
     * Init UI functions
     */         
    init: function () {
        GREENHOME.ajaxProductDetail();
        GREENHOME.commentsHelpTooltips();
        GREENHOME.contactHelpTooltips();
        GREENHOME.groupsNav();
        GREENHOME.articlePerex();
        GREENHOME.print();
        GREENHOME.productTabs();
        GREENHOME.watchingDogHelp();
        GREENHOME.paypalHelp();
        GREENHOME.codHelp();
        GREENHOME.configureHelp();
        GREENHOME.sets();
        GREENHOME.openInNewWindow();
        GREENHOME.autoSubmitForms();
        GREENHOME.setsForm();
        GREENHOME.zoomDetail();
        GREENHOME.searchTweak();
        GREENHOME.links();
        $(window).load(function() {
            GREENHOME.banner();
        });    
    },
    

    ajaxProductDetail: function () {
 
        $('.tag-cloud a, .set td a, .products-table td a').hoverIntent({
            sensitivity: 1,
            interval: 150,
            over: insertProductDetail,
            timeout: 100,
            out: removeProductDetail                
        });
        
        $('.ajax-detail-popup .close a').click(function() {
            removeProductDetail();
            return false; 
        });
        
        function insertProductDetail() {
            
            var element = this;
            var product_id = getProductId(element);
        
            $.get("/ajax/", {id: product_id}, function(data) {
                
                $("body").append(data);
                
                // Set position
                var offset =  $(element).offset();
                var top = offset.top - $('.ajax-detail-popup').height() - 70;
                var left = offset.left - 243;
               
                $('.ajax-detail-popup').css('top', top + 'px'); 
                $('.ajax-detail-popup').css('left', left + 'px');
            });
        }
        
        function removeProductDetail() {
            $('.ajax-detail-popup').remove();
        }        
        
        function getProductId(element) {
            var link = element;
            
            if ($(element).is('div')) {
                link = $(element).find('a'); 
            }
            var url = $(link).attr('href');
            return url.replace('/katalog/', '');
        }

    },    
    
    commentsHelpTooltips: function () {
        $(".comments .helptext").hide();
        $(".comments .help").tooltip({
           showURL: false,
           bodyHandler: function() {
             return $(this).parent().find(".helptext").html();
           },
           track: true
        })            
    
    },
    
    contactHelpTooltips: function () {
        $(".contactform .helptext").hide();
        $(".contactform .help").tooltip({
           showURL: false,
           bodyHandler: function() {
             return $(this).parent().find(".helptext").html();
           },
           track: true
        })            
    
    },
    
    groupsNav: function () {
        // Add open/close link inside every navigation item that has child UL
        var link = $('<a href="#" class="collapse"><img src="/images/icons/open2.gif" alt="+" width="9" height="9" /></a>')
        var links = $('.group-nav > li:has(ul)')
        links.addClass('collapsable')  
        links.prepend(link);
        
        // Add event handlers to link
        $('.group-nav > li > a.collapse').click(function() {
            var img = $(this).find('img');
            
            $(this).parent().find('ul').toggle();
            
            if (img.attr('alt') == '+') {
                 img.attr('alt', '-');
                 img.attr('src', '/images/icons/close2.gif');
            } else {
                 img.attr('alt', '+');
                 img.attr('src', '/images/icons/open2.gif');
            }
            return false;
        });
    },
    
    articlePerex: function () {
        
        var link = $('<a href="#" class="collapse"><img src="/images/icons/open2.gif" alt="+" width="9" height="9" /></a>')
          
        $('#content .related .perex').prepend(link);
        
        // Add event handlers to link
        $('#content .related .perex a.collapse').click(function() {
            var img = $(this).find('img');
            
            $(this).parent().toggleClass('perex-opened');
            
            if (img.attr('alt') == '+') {
                 img.attr('alt', '-');
                 img.attr('src', '/images/icons/close2.gif');
            } else {
                 img.attr('alt', '+');
                 img.attr('src', '/images/icons/open2.gif');
            }
            return false;
        });
    },    
    
    print: function () {
        $("#print").bind("click", function () {
            window.print();
            return false;
        });
    },
    
    productTabs: function () {    
        var $tabs = $("#tabs > ul").tabs(); 
        $('.product-detail .img a').click(function() { 
            $tabs.tabs('select', 3); 
        });
        $('.product-detail .right li.desc a').click(function() { 
            $tabs.tabs('select', 0); 
        });
    },
    
    watchingDogHelp: function () {
        $("#hlidaci-pes-napoveda").hide();
        $("#watching-dog").tooltip({
           showURL: false,
           bodyHandler: function() {
             return $("#hlidaci-pes-napoveda").html();
           },
           left: -200
        })    
    },

    paypalHelp: function () {
        $("#paypal-napoveda").hide();
        $("#paypal").tooltip({
           showURL: false,
           bodyHandler: function() {
             return $("#paypal-napoveda").html();
           },
           left: -200
        })    
    },

    codHelp: function () {
        $("#dobirka-napoveda").hide();
        $("#cod").tooltip({
           showURL: false,
           bodyHandler: function() {
             return $("#dobirka-napoveda").html();
           },
           left: -200
        })    
    },
    
    configureHelp: function () {
        $("#konfigurovat-napoveda").hide();
        $("#configure").tooltip({
           showURL: false,
           bodyHandler: function() {
             return $("#konfigurovat-napoveda").html();
           },
           left: -50
        })    
    },    
    
    sets: function () {
        $('.set .title select').bind("change", function () {
            var value = $(this).find('option:selected').attr('value');
            $(this).parents('tr').children('td:nth-child(3)').find('a').attr('href', '/katalog/' + value); 
        });
    },
    
    openInNewWindow: function () {
        $('a.new-window').click(function(){
            window.open(this.href);
            return false;
        });    
    },
    
    setsForm: function () {
        // Add target _blank to Print set button
        $("#sets-form .print-set").bind("click", function () {
            $("#sets-form").attr("target", "_blank")
        });
        
        // Add target _self to all other submits
        $("#sets-form :submit:not(.print-set)").bind("click", function () {
            $("#sets-form").attr("target", "_self")
        });
    },    
    
    autoSubmitForms: function () {
        $(".perpage .perpage-submit").hide();
        $(".ordering select, .perpage select").bind("change", function () {
            $(this).parents('form')[0].submit();
        });
    },
    
    zoomDetail: function () {
        $('#sidebar .detail a').bind('click', function () {
            $('.detail-popup').css("display", "block");
            return false;
        });
    },
    
    searchTweak: function () {
        $('#searchtext, #searchtext2').focus(function () {
            $(this).css('background', '#fff');
        });
        $("#searchtext[value!=''], #searchtext2[value!='']").css('background', '#fff');
        $('#nojs-search-url').hide();
    },

    links: function () {
        $("a[@href^=http]:not([@href*='" + GREENHOME.url  + "'])").addClass('link-out');
        $('a[@href^="mailto:"]').addClass('mail');
    },
    
    banner: function () {
        $("#banner").fadeIn(1000);
        $('#banner').cycle({
            speed: 2000,
            timeout: 7000,
            delay: 1000,
            cleartype: 1,
            autostop: 1,
            end: onEnd
        });
        
        function onEnd() {
            $("#banner").fadeOut(1000);
        }
    },
    
    /**
     * Preload images
     * @param {Array} images array with names of images   
     */               
    preloadImages: function (images) {
        for (var i = 0; i < images.length; i++) {
            var image = new Image();
            image.src = images[i];
        }
    } 
}

$(document).ready(function () {
    GREENHOME.init();
});

GREENHOME.preloadImages(["/images/bg/banner.gif"]);
