﻿var Blanche = window.Blanche || {};
Blanche.gallery = [];
Blanche.galleryCurrent = 0;
Blanche.galleryInterval = 12000;

(function ($) {

    $.fn.cycle.transitions.blanche = function($cont, $slides, opts) {
	    var d = opts.direction || 'left';
	    var w = $cont.css('overflow','hidden').width();
	    var h = $cont.height();
	    opts.before.push(function(curr, next, opts) {
            //last param reverses z-index
		    $.fn.cycle.commonReset(curr,next,opts,true,true,false);
		    if (d == 'right')
			    opts.cssBefore.left = -w;
		    else if (d == 'up')
			    opts.cssBefore.top = h;
		    else if (d == 'down')
			    opts.cssBefore.top = -h;
		    else
			    opts.cssBefore.left = w;
	    });
        opts.animOut.left = 0;
	    opts.animIn.left = 0;
	    opts.animIn.top = 0;
	    opts.cssBefore.top = 0;
	    opts.cssBefore.left = 0;
    };

    $(function () {
    
        // Search
        $('#search input:text').focus(function () {
            if ($.trim(this.value) == 'Search Blanchelande.co.uk') this.value = '';
            $(this).addClass('focus');
        }).blur(function () {
            if ($.trim(this.value) == '') {
                this.value = 'Search Blanchelande.co.uk';
            }
            $(this).removeClass('focus');
        });

        // Mega Menus
        function menuHoverOver() {            
            $(this).children('ul').animate({
                opacity: 1,
                height: 'toggle'
            }, 300);
        }

        function menuHoverOut() {
            //alert(1);
            $(this).children('ul').animate({
                opacity: 0,
                height: 'toggle'
            }, 100);
        }

        var config = {
            interval: 100,
            over: menuHoverOver,
            timeout: 500,
            out: menuHoverOut
        };

        $("#main ul li .submenu").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
        $("#main ul li").hoverIntent(config);   
               
        //Newsletter
        $("#nltextbox").focus(function () {
            if ($.trim(this.value) == 'Enter your email address') this.value = '';
            $(this).addClass('focus');
        }).blur(function () {
            if ($.trim(this.value) == '') this.value = 'Enter your email address';
            $(this).removeClass('focus');
        }).keypress(function (e) {
            if (e.which == 13) {
                $("#nlsubmit").click();
                return false;
            }
        });
        $("#nlsubmit").click(function () {
            var eml = $("#nltextbox").val();

            if ($.trim(eml) != '' && $.trim(eml) != 'Enter your email address') {
                $("div.nltext").empty().html("<h2>Sending registration...</h2><p>Please wait a moment.</p>");

                jQuery.post("/base/BlanchRest/NewsletterSignup", {
                    email: $.trim(eml)
                }, function (data) {
                    if (data == 'ok') {
                        $("div.nltext").empty().hide().html("<h2>Thank you</h2><p>You have been signed up.</p>").fadeIn(500);
                        $("#nltextbox").val('');
                    } else {
                        $("div.nltext").empty().hide().html('<h2 class="nlerror">Error</h2><p>' + data + '</p>').fadeIn(500);
                    }
                }
                );
            }
            else
                $("div.nltext").empty().hide().html('<h2 cslass="nlerror">Error</h2><p>Please enter an email address.</p>').fadeIn(500);

            return false;
        });
    });

    $(window).bind("load", function () {
        //home banners
        if ($('#active .bannerimg').length > 1) {
            $('body').append($('<span id="getWide" />'));
            $('#active .bannerimg').each(function () {
                Blanche.gallery.push($(this));
            });
            Blanche.gallery[0].fadeIn(750);
            setTimeout("Blanche.clipTextInDelayOne()", 500);
            setInterval("Blanche.swapBanner()", Blanche.galleryInterval);
        } else if ($('#active .bannerimg').length == 1) {
            $('body').append($('<span id="getWide" />'));
            $('#active .bannerimg').fadeIn(750);
            setTimeout("Blanche.clipTextInDelayTwo()", 500);
        }


        //Main Image transitions
//        $(".story.notfull").cycle({
//            fx: 'cover',
//            easing: 'easeOutExpo', 
//            speed: 1500,
//            timeout: 4000,
//            random: 1,
//            pause: 1
//        }); 

        $(".story.notfull").cycle({
            fx: 'blanche',
            clip: 'r2l',
            easing: 'easeOutExpo', 
            speed: 1600,
            timeout: 15000,
            random: 1,
            pause: 1
        }); 

    });
    
})(jQuery);

Blanche.swapBanner = function () {
    var curPos = Blanche.galleryCurrent;
    var nextPos;
    if ((Blanche.gallery.length - 1) == Blanche.galleryCurrent) nextPos = 0;
    else nextPos = Blanche.galleryCurrent + 1;

    Blanche.clipTextOut();

    Blanche.gallery[nextPos].css('z-index', 2).animate({
        left: 0
    }, 1500, 'easeOutExpo', function () {
        Blanche.gallery[curPos].css('left', 980);
        Blanche.gallery[nextPos].css('z-index', 1);
        Blanche.clipTextIn(Blanche.gallery[nextPos].children('span').text());
    });

    Blanche.galleryCurrent = nextPos;
}

Blanche.clipTextIn = function (txt) {
    $('#getWide').text(txt);
    var w = $('#getWide').outerWidth() + 10;
    $('#storyh4 span')
        .css('width', 0)
        .animate({ 'padding-left': 20, 'padding-right': 20 }, 75)
        .animate({ 'width': w }, 600, 'easeOutExpo', function () { $('#storyh4 span').text(txt); });
}
Blanche.clipTextOut = function () {
    $('#storyh4 span').empty()
        .animate({ 'width': 0, 'padding-left': 0, 'padding-right': 0 }, 200, 'easeInOutExpo');
}
Blanche.clipTextInDelayOne = function () { Blanche.clipTextIn(Blanche.gallery[0].children('span').text()); }
Blanche.clipTextInDelayTwo = function () { Blanche.clipTextIn($('#active .bannerimg').children('span').text());  }
