// JavaScript Document
function customedScroll(){
    $("#gallery-category-photos").jScrollPane({
        showArrows: true
    });
}

function setFBUrl(){
    var windowCurrentUrl = window.location.href;

    if(windowCurrentUrl.match("auckland")){
        $("#logo").hide();
        //$("#logo img").attr("src","images/logo-akl.png");
        $("#logo").css("background","url(images/logo.png) no-repeat 0px -275px");
        $("#fbSocialLink").attr("href","https://www.facebook.com/CalendarGirlsAKL");
        $("#regionLoc").html("auckland");
        $("#logo").show();
    }

    if(windowCurrentUrl.match("christchurch")){
        $("#logo").hide();
        //$("#logo img").attr("src","images/logo-chc.png");
        $("#logo").css("background","url(images/logo.png) no-repeat 0px -139px");
        $("#fbSocialLink").attr("href","http://www.facebook.com/CalendarGirlsChristchurch");
        $("#regionLoc").html("christchurch");
        $("#logo").show();
    }
    
    if(windowCurrentUrl.match("wellington")){
        $("#logo").hide();
        //$("#logo img").attr("src","images/logo-chc.png");
        $("#logo").css("background","url(images/logo.png) no-repeat 0px 0px");
        $("#fbSocialLink").attr("href","http://www.facebook.com/CalendarGirlsWellington");
        $("#regionLoc").html("wellington");
        $("#logo").show();
    }
}

function setContactForm(){
    var regionLoc =  $("#regionLoc").html();

    if(regionLoc == "auckland"){
        $("#mainMenu a").each(function(){
            var aHtml = $(this).html();
            aHtml = aHtml.toLowerCase();

            if(aHtml.match("contact")){
                $(this).attr("href","site/webpages/general/auckland-contact-information");
            }

            if(aHtml.match("about")){
                $(this).attr("href","site/webpages/auckland/about-calendar-girls");
            }
        });
    }

    if(regionLoc == "christchurch"){
        $("#mainMenu a").each(function(){
            var aHtml = $(this).html();
            aHtml = aHtml.toLowerCase();

            if(aHtml.match("contact")){
                $(this).attr("href","site/webpages/general/christchurch-contact-information");
            }

            if(aHtml.match("about")){                
                var chcABoutPage = $("#categoryChristchurch").html();
                $(this).attr("href","site/webpages/christchurch/"+chcABoutPage);
            }
        });
    }
    
    if(regionLoc == "wellington"){
        $("#mainMenu a").each(function(){
            var aHtml = $(this).html();
            aHtml = aHtml.toLowerCase();

            if(aHtml.match("contact")){
                $(this).attr("href","site/webpages/general/wellington-contact-information");
            }

            if(aHtml.match("about")){                
                var wgtnABoutPage = $("#categoryWellington").html();
                $(this).attr("href","site/webpages/wellington/"+wgtnABoutPage);
            }
        });
    }
}

function setContactActiveMenu(){   
    var windowCurrentUrl = window.location.href;

    if(windowCurrentUrl.match("site/webpages/general/christchurch-contact-information")){
        $("#mainMenu a").each(function(){
            var aHref = $(this).attr("href");

            if(aHref == "site/webpages/general/christchurch-contact-information"){
                $("#mainMenu li").each(function(){
                    if($(this).hasClass("activeMenu")){
                        $(this).removeClass("activeMenu");
                    }
                });

                $(this).parent().addClass("activeMenu");
            }
        });
    }   
    
    if(windowCurrentUrl.match("site/webpages/general/wellington-contact-information")){
        $("#mainMenu a").each(function(){
            var aHref = $(this).attr("href");

            if(aHref == "site/webpages/general/wellington-contact-information"){
                $("#mainMenu li").each(function(){
                    if($(this).hasClass("activeMenu")){
                        $(this).removeClass("activeMenu");
                    }
                });

                $(this).parent().addClass("activeMenu");
            }
        });
    } 
}

function setRegionSession(){
    var regionSession = $("#regionLoc").html();
    
    if(regionSession.match("auckland")){
        $("#logo").hide();
        //$("#logo img").attr("src","images/logo-akl.png");
        $("#logo").css("background","url(images/logo.png) no-repeat 0px -275px");
        $("#fbSocialLink").attr("href","https://www.facebook.com/CalendarGirlsAKL");
        $("#regionLoc").html("auckland");
        $("#logo").show();
    }

    if(regionSession.match("christchurch")){
        $("#logo").hide();
        //$("#logo img").attr("src","images/logo-chc.png");
        $("#logo").css("background","url(images/logo.png) no-repeat 0px -139px");
        $("#fbSocialLink").attr("href","http://www.facebook.com/CalendarGirlsChristchurch");
        $("#regionLoc").html("christchurch");
        $("#logo").show();
    }
    
    if(regionSession.match("wellington")){
        $("#logo").hide();
        //$("#logo img").attr("src","images/logo-chc.png");
        $("#logo").css("background","url(images/logo.png) no-repeat 0px 0px");
        $("#fbSocialLink").attr("href","http://www.facebook.com/CalendarGirlsWellington");
        $("#regionLoc").html("wellington");
        $("#logo").show();
    }
}

function setChcRegionMainMenu(){
    if( $("#regionLoc").html() == "christchurch"){
        $("#mainMenu a").each(function(){
            var href = $(this).attr("href");
            if(href.match("auckland")){
                var newHref = href.replace("auckland","christchurch");
                
                $(".categoryPageChristchurch").each(function(){
                    var strNewHref = newHref.split("/");
                    var lastStr = strNewHref[strNewHref.length - 1];
                    
                    var thisHtml = $(this).html();                    
                    thisHtml = thisHtml.substr(0,lastStr.length);
                    
                    if(newHref.match(thisHtml)){
                        newHref = newHref.substr(0,newHref.length - (lastStr.length)) + $(this).html();
                    }
                });               
                
                $(this).attr("href",newHref);
            }
        });

        var windowCurrentUrl = window.location.href;
        $("#mainMenu a").each(function(){
            var mHref = $(this).attr("href");
            if(windowCurrentUrl.match(mHref)){
                $("#mainMenu li").each(function(){
                    if($(this).hasClass("activeMenu")){
                        $(this).removeClass("activeMenu");
                    }
                });

                $(this).parent().addClass("activeMenu");
            }
        });
    }
}

function setWgtnRegionMainMenu(){
    if( $("#regionLoc").html() == "wellington"){
        $("#mainMenu a").each(function(){
            var href = $(this).attr("href");
            if(href.match("auckland")){
                var newHref = href.replace("auckland","christchurch","wellington");
                
                $(".categoryPageWellington").each(function(){
                    var strNewHref = newHref.split("/");
                    var lastStr = strNewHref[strNewHref.length - 1];
                    
                    var thisHtml = $(this).html();                    
                    thisHtml = thisHtml.substr(0,lastStr.length);
                    
                    if(newHref.match(thisHtml)){
                        newHref = newHref.substr(0,newHref.length - (lastStr.length)) + $(this).html();
                    }
                });               
                
                if(newHref){
                    $(this).attr("href",newHref);
                }
            }
        });

        var windowCurrentUrl = window.location.href;
        $("#mainMenu a").each(function(){
            var mHref = $(this).attr("href");
            if(windowCurrentUrl.match(mHref)){
                $("#mainMenu li").each(function(){
                    if($(this).hasClass("activeMenu")){
                        $(this).removeClass("activeMenu");
                    }
                });

                $(this).parent().addClass("activeMenu");
            }
        });
    }
}

$(function(){
    if($.flash.available){
        //flash slide slideshow
        /*
        $('#homeslideshow').html(''); //clear alternative content
        $('#homeslideshow').flash({
            swf: liveSite+'images/home_slideshow.swf',
            menu: "false",
            wmode: "transparent",
            width: "625",
            height: "250",
            flashvars: {
                clickTag: liveSite+'slideshow'
            }
        });
        */
		
        //magazine flash viewer
        $('#magazine-flash-viewer').html(''); //clear alternative content
        $('#magazine-flash-viewer').flash({
            swf: liveSite+'images/magazine.swf',
            menu: "false",
            wmode: "transparent",
            width: "890",
            height: "615",
            flashvars: {
                clickTag: liveSite,
                issue: window.magazineIssue
            }
        });
    }

    $("#sub-category a").click(function(event){
        event.preventDefault();
        
        $("#sub-category a").each(function(){
            $(".activeGalleryMenu").removeClass("activeGalleryMenu");
            $(this).css("color","#ffffff");
        });
        
        $(this).addClass("activeGalleryMenu");

        $(".activeGalleryMenu").css("color","#c48415");

        var categoryName = $(this).find("span").html();

        $(".gallery-category-desc-box").each(function(){
            if(categoryName == $(this).attr("id")){
                $(".gallery-category-desc-box").each(function(){
                    $(this).hide();                    
                });

                $(this).show();                
            }
        });

        $(".photoUL").each(function(){
            if(categoryName == $(this).find("li").attr("class")){
                $(".photoUL").each(function(){
                    $(this).hide();
                    $(this).find("a").attr("rel","");
                });

                $(this).show();
                $(this).each(function(){
                    $(this).find("a").attr("rel","prettyPhoto[pp_gal]");
                });
            }
        });

        $(".photoUL").find("a").click(function(event){
            event.preventDefault();
        });

        $("a[rel^='prettyPhoto']").prettyPhoto({
            animationSpeed:'slow',
            theme:'dark_square'
        });

        customedScroll();
    });
    
    $("a[rel^='prettyPhoto']").prettyPhoto({
        animationSpeed:'slow',
        theme:'dark_square'
    });

    //Search box
    $('#searchFor').focus(function(){
        $(this).css('color','#000000');
        $(this).attr('value','');
    });

    //Controll the main menu drop down list
    $('ul li',$('#mainMenu')).hover(
        function(){
            $(this).addClass('over');
        },
        function(){
            $(this).removeClass('over');
        }
        );

    $(window).ready(function(){
        $("<div class='menu-active-bg' style='float:left;height:64px;margin:2px 0px 0px 0px;width:3px;position:absolute;left:0px;'><img src='images/menu-active-left.png' alt=''/></div>").insertBefore(".activeMenu a");
        $("<div class='menu-active-bg' style='float:left;height:64px;margin:2px 0px 0px 0px;width:4px;position:absolute;right:0px;'><img src='images/menu-active-right.png' alt=''/></div>").insertAfter(".activeMenu a");
    });

    $("#mainMenu li a").mouseover(function(){       
        $(".menu-active-bg").each(function(){
            if($(this).parent().hasClass("activeMenu")){
            }else{
                $(this).remove();
            }
        });

        $("<div class='menu-active-bg' style='float:left;height:64px;margin:2px 0px 0px 0px;width:3px;position:absolute;left:0px;'><img src='images/menu-active-left.png' alt=''/></div>").insertBefore($(this));
        $("<div class='menu-active-bg' style='float:left;height:64px;margin:2px 0px 0px 0px;width:4px;position:absolute;right:0px;'><img src='images/menu-active-right.png' alt=''/></div>").insertAfter($(this));
    }).mouseout(function(){
        $(".menu-active-bg").each(function(){
            if($(this).parent().hasClass("activeMenu")){
            }else{
                $(this).remove();
            }
        });
    });    
    
    //Make the shipping details same as billing details
    $('#shippingController').click(function(){
        var theForm=document.forms['cart'];
        var billings = ['#bill_firstname','#bill_lastname','#bill_company','#bill_address', '#bill_address1',
        '#bill_city', '#bill_state', '#bill_postcode', '#bill_country','#bill_phone','#bill_email'];
        var deliveries = ['#delivery_firstname','#delivery_lastname','#delivery_company','#delivery_address', '#delivery_address1',
        '#delivery_city', '#delivery_state', '#delivery_postcode', '#delivery_country','#delivery_phone','#delivery_email'];
			
        for( var i=0; i< billings.length; i++ )
        {
            if($('#shippingController').attr('checked'))
            {
                $(deliveries[i]).attr('value', $(billings[i]).attr('value'));
            }
        }
    });
		
    //Checkout confirm form
    $('#checkoutConfirmForm').submit(function(){
        if($('#terms-and-conditions').attr('checked')){
            return true;
        }else{
            alert('Please read and agree to the terms and conditions before the order can be submitted!');
            return false;
        }
    });
		
    //User registration form verification
    $('#loginForm').submit(function(){
        if($('#member_pass').attr('value')!=$('#member_pass1').attr('value')){
            alert('Passwords entered are not the same!');
            return false;
        }
    });
		
    //product images lightbox
    $('#allImages a').lightBox();
		
    //Blog commment area
    $('.reply-button').click(function(){
        $('#cancel-reply').css('display','block');
											
        $('#parent_id').attr('value',$(this).attr('title'));
        $('#commentform-section').css('display','none');
        $(this).parent().append($('#commentform-section').remove());
        $('#commentform-section').slideDown('slow');
											
        $('#cancel-reply').click(function(){
            $('#cancel-reply').css('display','none');
            $('#parent_id').attr('value',0);
            $('.comment-section').append($('#commentform-section').remove());
            return false;
        });
											
        return false;
    });
});


//Animate the product detail slideshow images.
$(function(){
    var imagesUrl=Array();
    var mainImage=$('img').filter('#mainImage');
    var current=0;
    var interval=null;
    var intervalTime=5000;
    var thumbnailImages=$('img').filter('.thumbnail');
	
    thumbnailImages.each(function(i){
        var theSrc=this.src;
        var otherImagesUrl=Array();
		
        otherImagesUrl[0]=theSrc.replace(/thumb_/,'medium_');
        otherImagesUrl[1]=theSrc.replace(/thumb_/,'');
        imagesUrl[i]=otherImagesUrl;
		
        $(this).click( function(){
            clearInterval(interval);
            mainImage.attr('src',imagesUrl[i][0]).fadeIn();
            $('#theLink').attr('href',imagesUrl[i][1]);
				
            current=i;
            if(imagesUrl.length>1)//start slide show for over 1 image.
                interval=setInterval(slideShow,intervalTime);
            //The thumbnail class switcher
            thumbnailImages.each(function(i){
                if(i!=current)
                    $(this).removeClass('thumbnail-over');
                else
                    $(this).addClass('thumbnail-over');
            });

        });
		
        $(this).mouseover( function(){
            if(current!=i)
                $(this).addClass('thumbnail-over');
        }
        );

        $(this).mouseout( function(){
            if(current!=i)
                $(this).removeClass('thumbnail-over');
        }
        );
    });

    function slideShow(){
        if(current<(imagesUrl.length-1))
            current++;
        else
            current=0;
			
        mainImage.attr('src',imagesUrl[current][0]).fadeIn();
        $('#theLink').attr('href',imagesUrl[current][1]);
			
        thumbnailImages.each(function(i){
            if(i!=current)
                $(this).removeClass('thumbnail-over');
            else
                $(this).addClass('thumbnail-over');
        });
			
    }
	
	
    $(thumbnailImages.get(0)).addClass('thumbnail-over');
	
    if(imagesUrl.length>1) //start slide show for over 1 image.
        interval=setInterval(slideShow, intervalTime);
});
