﻿/***

Blueroom V4 Preference capture js
2008 www.aislondon.com

***/
(function($) {

    /* 
    Scope
    This initialises the namespace if it has not been initialised elsewhere
    */
    $.blueroom = $.blueroom || {};

    /*
    Global fields
    Any global objects that you need access to are defined here
    */
    var g_ready = false;
    var g_self = this;

    /* 
    API methods
    If the class has any API methods then put them here
    (methods that can be called directly on objects e.g. $("body").customMethod();
    */
    $.fn.preferences = function() {
        return this.each(function() {
            //alert("API method called on " + this);
        });
    };

    /*
    Class
    */
    $.blueroom.home = function(options) {
        var c_self = this;
        var c_ready = false;
        c_self.options = $.extend({}, $.blueroom.home.defaults, options);

        /*
        Instance methods
        */
        c_self.functions = {
            interval: function() {
                /*
                Insert tasks to be repeated at set intervals
                */
                c_self.functions.debuglog("interval event");
                setTimeout(c_self.functions.interval, c_self.options.intervalFrequency);
            },
            initialise: function(context) {
                context.each(function(index) {
                    /*
                    Handle initialisation
                    Should be called on page load and on new content loaded through ajax, so pay attention to the context.
                    */
                    var currentContext = $(this);

                    currentContext.find(".nothanksbutton").click(function() {
                        /*AJM: We need to switch everything over to absolute positioning, relative to the bottom of the element, so that when we slide the panel up, all items slide up with it at the same time*/
                        var height = $("#alert").height();
                        $("#alert").css({ height: height });
                        var bottom = $("#alert h2").offsetParent().innerHeight() - $("#alert h2").position().top - $("#alert h2").innerHeight();
                        var bottom2 = $("#alert p").offsetParent().innerHeight() - $("#alert p").position().top - $("#alert p").innerHeight();
                        $("#alert h2").css({ position: "absolute", bottom: bottom });
                        $("#alert p").css({ position: "absolute", bottom: bottom2 });

                        $("#alert").animate({ height: "0", paddingBottom: "0", paddingTop: "0"}, 500, function() {
                             $("#alert").css({ marginBottom: "0" });
                        });
                       // return false;
                    });

                    currentContext.find(".eventsList").each(function() {
                        var eventsList = $(this);
                        var eventsListDetails = eventsList.children('.eventsList-detail');
                        var eventsListItems = eventsList.children('.eventsList-items');
                        var leftScroller = $("<div><a href=\"#\"></a></div>");
                        leftScroller.addClass("leftscroller");
                        //leftScroller.find("span").css("opacity", "0.2");//.addClass("clickable");
                        var rightScroller = $("<div><a href=\"#\"></a></div>");
                        rightScroller.addClass("rightscroller");
                        //rightScroller.find("span").css("opacity", "0.2"); //.addClass("clickable");

                        var shadowLeft = $("<div></div>");
                        shadowLeft.addClass("shadowleft");
                        var shadowRight = $("<div></div>");
                        shadowRight.addClass("shadowright");
                        
                        // TB: Testing another binding method,
                        //		for workaround with IE rounded corners plugin
                        // eventsList.prepend(leftScroller).prepend(shadowLeft).append(rightScroller).append(shadowRight);
                        
                        // TB: Seems to work nice so far... requires an extra 3 lines though...
                        eventsListDetails.before(leftScroller).before(shadowLeft);
                        eventsListItems.after(rightScroller).after(shadowRight);			

                        eventsList.pngFix();

                        leftScroller.find("a").click(function() {
                            c_self.functions.scrollLeft(eventsList.find(".eventsList-items"));
                            return false;
                        });
                        rightScroller.find("a").click(function() {
                            c_self.functions.scrollRight(eventsList.find(".eventsList-items"));
                            return false;
                        });
                        //init scrollbar by calling scrollLeft
                        c_self.functions.scrollLeft(eventsList.find(".eventsList-items"));
                    });

                    currentContext.find(".eventsList-items").css({ "overflow": "hidden", position: "relative", "right": "31px", "width": "749px" }); //relative needed to fix ie overflow bug
                    currentContext.find(".eventsList-items ul").each(function(i) {
                        var currentItems = $(this);
                        var width = (currentItems.find("li").size() + 1) * 195;
                        currentItems.css({ "width": width + "px", "position": "relative", left: "0px" });

                        //c_self.functions.scrollLeft(currentItems);
                        
                    });
                    currentContext.find(".eventsList").each(function(){
						$(this).css("height",$(this).children('.eventsList-items').height());
						//alert($(this).height())
                    });
                    
                    var IE6height = $(".outer-content-first ul").height() + 12;
                    $(".outer-content-first:not(#alert)").height(IE6height)
                    

                });

            },
            scrollLeft: function(context) {
                /*var currentLeft = $(context).scrollLeft();
                var currentItem =  currentLeft / 195 + 1;
                alert(Math.round(currentItem));
                var itemsToScroll = ($(context).find("li").size() - 3);
                //var left = itemsToScroll * 195;
                var speed = itemsToScroll * 300;
                $(context).animate({ scrollLeft: "0" }, speed, function() {
                //c_self.functions.scrollLeft(context);
                });*/
                c_self.functions.scroll(context, "left");
            },
            scrollRight: function(context) {
                /*var itemsToScroll = ($(context).find("li").size() - 3);
                var left = itemsToScroll * 195;
                var speed = itemsToScroll * 300;
                $(context).animate({ scrollLeft: left }, speed, function() {
                //c_self.functions.scrollRight(context);
                });*/
                c_self.functions.scroll(context, "right");
            },
            scroll: function(context, direction) {

                var currentLeft = $(context).scrollLeft();
                var currentItem = Math.round(currentLeft / 195) + 1;
                var totalItems = $(context).find("li").size();
                var itemToScrollTo;
                if (direction == "right") {
                    itemToScrollTo = Math.min(totalItems - 2, currentItem + 3);
                }
                else if (direction == "left") {
                    itemToScrollTo = Math.max(1, currentItem - 3);
                }
                var itemsToScroll = Math.sqrt((currentItem - itemToScrollTo) * (currentItem - itemToScrollTo));
                var pixelsToScrollTo = (itemToScrollTo - 1) * 195;
                var speed = itemsToScroll * 300;
                /*alert(
                "currentLeft: " + currentLeft
                + " currentItem: " + currentItem
                + " totalItems: " + totalItems
                + " itemToScrollTo: " + itemToScrollTo
                + " itemsToScroll: " + itemsToScroll
                + " pixelsToScrollTo: " + pixelsToScrollTo
                + " speed: " + +speed);*/

                c_self.functions.updateScrollButtons(context, itemToScrollTo, totalItems);


                $(context).animate({ scrollLeft: pixelsToScrollTo }, speed, function() {
                });
            },
            updateScrollButtons: function(context, itemToScrollTo, totalItems) {
                if (itemToScrollTo == 1) {
                    $(context).parent().find(".leftscroller a").removeClass("clickable")/*.animate({ "opacity": "0.2" })*/;
                    if (totalItems >= 4) {
                        $(context).parent().find(".rightscroller a").addClass("clickable")/*.animate({ "opacity": "1" })*/;
                    }
                    else {
                        $(context).parent().find(".rightscroller a").removeClass("clickable")/*.animate({ "opacity": "0.2" })*/;
                    }
                }
                else if (itemToScrollTo >= totalItems - 2) {
                    $(context).parent().find(".rightscroller a").removeClass("clickable")/*.animate({ "opacity": "0.2" })*/;
                    if (totalItems >= 4) {
                        $(context).parent().find(".leftscroller a").addClass("clickable")/*.animate({ "opacity": "1" })*/;
                    }
                    else {
                        $(context).parent().find(".leftscroller a").removeClass("clickable")/*.animate({ "opacity": "0.2" })*/;
                    }
                }
                else {
                    $(context).parent().find(".leftscroller a").addClass("clickable")/*.animate({ "opacity": "1" })*/;
                    $(context).parent().find(".rightscroller a").addClass("clickable")/*.animate({ "opacity": "1" })*/;
                }
            },
            preload: function() {
                c_self.functions.debuglog("preload");
                c_self.functions.preloadImages(/*insert array of images here*/);
            },
            preloadImages: function() {
                for (var i = 0; i < arguments.length; i++) {
                    $("<img>").attr("src", arguments[i]);
                }
            },
            debuglog: function(logtext) {
                /*try {
                    if (console && console.log) {
                        console.log(logtext);
                    }
                }
                catch (err) {
                    //alert(err);
                }*/
            }
        };

        c_self.functions.initialise($("body"));
        c_ready = true;
    };

    // defaults
    $.blueroom.home.defaults = {
        //TODO:
        easing: "quad",
        animationSpeed: "1000",
        intervalFrequency: "10000"
    };

    // optional - initialise one or more instances of the class
    $(document).ready(function() {
        new $.blueroom.home();
        $("#divSignUp .panel input").css("display", "block");
    });
    

   })(jQuery);
   
   
   function AnimatePanel(){
   


    $("#divSignUp .panel input").css("display", "block");
    pnlUserHeight = $('#divSignUp').height();
			 		$('#divSignUp').css({ 'height': pnlUserHeight });
  $('#divSignUp').animate({ height: "0" }, 750, "easeInQuint", function() { $('#divSignUp').css({ 'overflow': 'hidden' }); });
   };