/*
Copyright (c) 2009, Swati Raju. All rights reserved.
Author: Swati Raju
version: 1.0.0
*/
var Y = YAHOO;
var yud = YAHOO.util.Dom;
var yue = YAHOO.util.Event;
var yua = YAHOO.util.Anim;
Y.namespace("LBF");
Y.namespace("LBF.indexPage");
/**
    * LBF
    *
    * @module LBF
    * @title LBF
    * @namespace YAHOO.LBF
    * @requires YAHOO, dom, event
    */
Y.LBF.indexPage = function() {

    var pause       = 1;
    var current     = 0;
    var loop        = null;
    var labels      = ["STOP BY OUR NEW STEINER ST. SHOP!",
                        "HEALTHY FOR YOU AND FOR THE PLANET",
                        "DELICIOUS DOGS BY THE BAY",
                        "GRILL UP SOME GRASS-FED GOODNESS",
                        "GET THE SHIRT OFF FRANK'S BACK",
                        "THE DETAIL'S IN THE DEVIL"
                        ];
    var links      = ["locations_shop.php",
                        "food_overview.php",
                        "locations_cart.php",
                        "products_dogs.php",
                        "products_merchandise.php",
                        "products_condiments.php"
                        ];
    var wait        = null;
    /**
    * Show
    *
    * @method show
    * @param {e} event
    * @returns
    */
    var play = function(e) {
        if (!pause){ 
            window.clearInterval(loop); 
            yud.removeClass("go", "pause");
        }
        else {
            loop = window.setInterval( function(){
                showNext();
            }, 7000);
            yud.addClass("go", "pause");
        }
        pause = !pause;
        if (e){ yue.preventDefault(e); }
    };
    /**
    * Show
    *
    * @method show
    * @param {e} event
    * @returns
    */
    var showNext = function() {
        if (current == 5){ current = 0; }
        else { current = current + 1; }
        show(null, current);
    };
    /**
    * Show
    *
    * @method show
    * @param {e} event
    * @returns
    */
    var show = function(e, id) {
        var id = id;
        current = id;
        var controls = yud.get("controls");
        var aCont = controls.getElementsByTagName("a");
        for (var i=0; i < aCont.length; i++){ yud.removeClass(aCont[i], "selected"); }
        yud.addClass(aCont[id], "selected");
        yud.setStyle("fade", "opacity", 0);
        var myAnim = new yua("fade", { 
                opacity: {to: .5}  
            }, .40, YAHOO.util.Easing.easeBoth);
        myAnim.onComplete.subscribe(function() { 
            var content = '<img src="http://www.letsbefrankdogs.com/static/imgs/carousel/lbf' + (id + 1) + '.png"/>'; 
            content += '<a href="' + links[id] + '"><div><div class="car' + (id + 1 ) + '"></div><span>' + labels[id] + '</span></div></a>';
            yud.get("car-images").innerHTML = content;
            var myAnim1 = new yua("fade", { 
                    opacity: {to: 0}  
                }, .40, YAHOO.util.Easing.easeBoth);
            myAnim1.animate();
        });
        myAnim.animate();
        if (e){ yue.preventDefault(e); }
    };
    /**
    * show video
    *
    * @method showVideo
    * @param
    * @returns
    */
    var showVideo = function(e) {
        var content        = yud.get("lb-content");
         content.innerHTML  = "";
         wait = new YAHOO.widget.Panel("wait",  
                    { width: "500px", 
                      fixedcenter: true, 
                      close: true, 
                      draggable: false, 
                      zindex:4,
                      modal: true,
                      visible: true
                     } 
                 );
        wait.setHeader("<h2><span><span>FRANK ON FILM</span></span></h2><a href='#' id='lb-close' class='close'></a>");
        var html = "<div class='lb-video' id='cbs'><object width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/K1QwAEgdn10&hl=en_US&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/K1QwAEgdn10&hl=en_US&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object></div>";
        
        wait.setBody(html);
        wait.render(document.body);
        wait.show();
        yue.on('lb-close', "click", closeVideo);
        yue.on("wait_mask", "click", closeVideo);
        yue.on('tw-sf', "click", closeVideo);
        yue.on("tw-la", "click", closeVideo);
        yue.preventDefault(e);
    }
    /**
    * close video
    *
    * @method closeVideo
    * @param
    * @returns
    */
    var closeVideo = function(e) {
        wait.hide();
        yud.get('vendr').innerHTML = "";
        wait.destroy();
        wait = null;
        yue.preventDefault(e);
    }
    return{
        /**
        * init page
        *
        * @method init
        * @param
        * @returns
        */
        init: function() {
            var go = yud.get("go");
            yue.on(go, "click", play, null, true);
            yue.on("cont0", "click", show, 0, false);
            yue.on("cont1", "click", show, 1, false);
            yue.on("cont2", "click", show, 2, false);
            yue.on("cont3", "click", show, 3, false);
            yue.on("cont4", "click", show, 4, false);
            yue.on("cont5", "click", show, 5, false);
            play();
            yue.on("vid", "click", showVideo, null, false);
        }
    };
}();
Y.LBF.indexPage.init();