//Last updated 17-12-09

var ofLst;
var ofLstCount;
var ofLst;
var spOffInt;

//sub nav array
/*
var docNames=[];
docNames['self-storage']=0;
docNames['self-storage/tipsandadvice-personal']=0;
docNames['unit-size']=0;
docNames['self-storage/faq-personal']=0;
docNames['self-storage/testimonials-personal']=0;

docNames['business-storage.htm']=1;
docNames['archiving.htm']=1;
docNames['podsize-business.htm']=1;
docNames['testimonials-business.htm']=1;
docNames['inthepress.htm']=1;


docNames['packitup.htm']=2;
docNames['boxes.htm']=2;
docNames['bubblewrap.htm']=2;
docNames['valuepacks.htm']=2;
docNames['everythingelse.htm']=2;

docNames['howitworks.htm']=3;
docNames['tipsandadvice-howitworks.htm']=3;
docNames['hiwpodsize.htm']=3;
docNames['faq-howitworks.htm']=3;

docNames['findus.htm']=4;
docNames['aboutus.htm']=4;
docNames['contact.htm']=4;
*/
var ddh = new Array();
var imgDisp=0;
var titleStr="";
var IsRunnin=0;
var imgInt;
var moInt;
var soCsi;
var spOffInt;

$(document).ready(function(){

    $('#overlay').hide();

    if($('#faq').length>0) {
        $('#faq').accordion({
            addsClass: "current",
            effect: "slide"
        });
    /*var ah = $('#faq h1');
	var as = $('#faq div');
	as.slideUp();
	ah.click(function(){
		ti = ah.index(this);
		if($(this).hasClass('current')){as.slideUp();$(this).removeClass('current');}
		else{as.slideUp().eq(ti).slideDown();ah.removeClass('current').eq(ti).addClass('current');}
	});*/
    }


    $('#personal').corner('tl');
    $('#faq h1').corner('tr');
    $('.cornerWrap').corner('top cc:#fff').corner('bottom cc:#16836e');
    $('#ot, .subbox, #maincontent, #ifbox').corner();
    $('.spnSpn').corner('5px');

    $('.videocontainer').corner();
    $('.tpSubNavC').animate({
        opacity:0
    },{
        duration:0,
        queue:false
    }).css({
        display:'block'
    });

    $('.qNfo p').formValidate();

    $('a').click(function(){
        thisLink=$(this).attr('href');
        thisRel=$(this).attr('rel');
        if(thisLink!="" && thisLink!=" " && thisLink!="#")
        {
            if(thisRel=="nw"){
                window.open(thisLink);
            }else{
                window.location=thisLink;
            }
        }
    return false;
    });

$(window).bind("load", function(){
    $('.jquery-corner').css({
        zIndex:1
    });

    //return document name
    /*docNme=CurrentDocName();
    thsNavIndx=docNames[docNme];*/
	
	
    if(thsNavIndx==undefined){
        thsNavIndx=5
    }

    //process images
    $('#images .imgSec img').animate({
        opacity:0
    },{
        duration:0,
        queue:false
    }).css({
        display:'block'
    });

    if(thsNavIndx==5){
        $('#images .imgSec').eq(thsNavIndx).imgCrossFade();
    }
    else{
        //load sub nav
        if(thsNavIndx!=undefined){
            $('#overlay').show();
            clearInterval(moInt);

            $('.tpNav li a').css({
                backgroundPosition:'bottom left'
            })
            .eq(thsNavIndx).css({
                backgroundPosition:'top left'
            });

            $('.tpSubNav').css({
                display:'none'
            });
            $('.tpSubNavC').animate({
                opacity:.9
            },200,function(){
                $('.tpSubNav').eq(thsNavIndx).css({
                    display:'block'
                });
                $('#overlay').hide();
            });

            $('#images .imgSec').eq(thsNavIndx).imgCrossFade();
        }
    }


    //QUOTE ACCORD
    $('.quoteAccHead, .podSc, .dteDur, .qNfo .spOffC').corner()
    //$('.quoteAccHead a').corner('tr br');

    $('.secLft select').html("<option>Loading sizes</option>");
    $('.steSel').html('<p>Loading dates</p>');

    $.ajax({
        type: "POST",
        url: "/media/scripts/php/process.php",
        data: "func=returnSqFt",
        success:function(data){
            expDta=data.split("{spl}");
            $('.secLft select').html(expDta[0]);
            $('.dteDur .steSel').html(expDta[1]);
            $('.qNfo .qSum').html(expDta[2]);
            $('.qNfo .spOffC').html(expDta[3]);

            $().returnSzeOpts($('.podSc select').eq(0).val());

            $('.podSc select').change(function(){
                thisVal=$(this).val();

                $().returnSzeOpts(thisVal);
            });
        }
    });

    $('.qNfo input[type="text"]').frmImps();

    accHeight=$('.quoteAccContent').eq(0).children('.quoteAccInner').height()+10;
    $('.quoteAccContent').eq(0).animate({height:accHeight},{duration:300,queue:false });


    $('.quoteAccHead a').click(function(){
        accIndx=$('.quoteAccHead a').index(this);
        thsRel=$(this).attr('rel');
        nxtAni=-1;
        aniSec=true;
        secSection=$('.quoteAccHead').eq(accIndx).children('h2').children('.qaHead')

        if(accIndx==0){
            secSection.returnPodHeadSelectNfo();
        }
        else if(accIndx==1){
            secSection.returnDteHeadSec();
        }

        if(thsRel==0){
            $('.quoteAccHead').eq(accIndx).addClass('greSec').children('h2').children('a').text('change').attr('rel',1);
            accIndx++;
            $('.quoteAccHead').eq(accIndx).removeClass('grSec');
        }
        else if(thsRel==1){
            $('.quoteAccHead').eq(accIndx).removeClass('greSec').addClass('gOrSec')
            .children('h2').children('a').text('amend').attr('rel',2);

            $('.quoteAccHead').each(function(){
                eaIndx=$('.quoteAccHead').index(this);
                eaRel=$(this).children('h2').children('a').attr('rel');
                if(eaRel==2 && eaIndx!=accIndx){
                    $(this).removeClass('gOrSec').addClass('greSec')
                    .children('h2').children('a').text('change').attr('rel',1);
                }
            });

        }
        else if(thsRel==2){
            $('.quoteAccHead').eq(accIndx).removeClass('gOrSec').addClass('greSec')
            .children('h2').children('a').text('change').attr('rel',1);

            $('.quoteAccHead a').each(function(){
                eaIndx=$('.quoteAccHead a').index(this);
                eaRel=$(this).attr('rel');
                if(eaRel==0 || eaRel==4 && nxtAni==-1){
                    nxtAni=eaIndx;
                    accIndx=eaIndx;
                }
            });
        }
        else if(thsRel==4){
            itmsCount=$('.qNfo .quReq').size();
            quI=0;

            //validate
            $('.qNfo .quReq').each(function(){
                thsVal=$(this).val();
                thsNme=$(this).attr('name');
                if(thsVal!=""){
                    if(thsNme=="eml"){if(CheckEmail(thsVal)){quI++;}}else{quI++;}
                }
            });

        if($('.qNfo p').frmSubmit()){
            //validate are you human
            humanInp=$('.qNfo .ayhInpBx').val()
            $.ajax({
                type: "POST",
                url: "/media/scripts/php/process.php",
                data: "func=amihuman&val="+humanInp,
                success: function(data){
                    if(data){
                        //form 1 upper floor or ground floor
                        chkOup=$('.podSc input[type="radio"]:checked').val();

                        //form 2 duration
                        frm2=$('.dteDur select');

                        ecpArr=[$('.podSc select').val(),
                        chkOup,
                        frm2.eq(2).val()+"-"+frm2.eq(1).val()+"-"+frm2.eq(0).val(),
                        frm2.eq(3).val()];

                        $('.qNfo input[type="text"]').each(function(){
                            frm3Val=$(this).val();
                            ecpArr.push(frm3Val);
                        });

                        $('.qNfo .spOffC input[type="radio"]:checked').each(function(){
                            spOfVal=$(this).val();
                            ecpArr.push(spOfVal);
                        });

                        $('.qNfo select').each(function(){
                            selVal=$(this).val();
                            ecpArr.push(selVal);
                        });

                        $.ajax({
                            type: "POST",
                            url: "/media/scripts/php/process.php",
                            data: "func=sndEml&val="+ecpArr.toString(),
                            success: function(data){
                                dteExp=data.split("{spl}");
                                if(dteExp[0]=="1"){
                                    $('.qNfo .qSum').css({
                                        display:'none'
                                    });
                                    $('.qNfo h4').css({
                                        display:'none'
                                    });
                                    $('.qNfo .ThnkYou').html("");
                                    $('.quoteAccHead h2 a').remove();
                                    $('.quoteAccInner form input[type="text"]').val("");
                                    $().refreshSum();
                                    $('.quoteAccHead').eq(accIndx).removeClass('gOrSec').addClass('greSec')
                                    $('.quoteAccHead').eq(3).removeClass('grSec');

                                    $qteNfo="<p><strong>Thank you</strong><br />";
                                    $qteNfo=$qteNfo+"Please see below for your quote.</p>";
                                    $qteNfo=$qteNfo+"<p>Quote Ref: <strong>"+dteExp[1]+"</strong><br />";
                                    $qteNfo=$qteNfo+"Pod Size: <strong>"+dteExp[2]+"</strong><br />";
                                    $qteNfo=$qteNfo+"Floor: <strong>"+dteExp[3]+"</strong><br />";
                                    $qteNfo=$qteNfo+"Date From: <strong>"+dteExp[4]+"</strong><br />";
                                    $qteNfo=$qteNfo+"Duration: <strong>"+dteExp[5]+"</strong><br />";
                                    $qteNfo=$qteNfo+"Weekly Price: <strong>&pound;"+dteExp[6]+"</strong><br />";
                                    $qteNfo=$qteNfo+"Monthly Price: <strong>&pound;"+dteExp[7]+"</strong><p/>";
                                    $qteNfo=$qteNfo+"<p><strong>Note:</strong> A copy of this quotation has been "+ 
									"emailed to the email address you provided.</p>";
									$qteNfo=$qteNfo+'<p class="requestPod"><a href="#" class="reservePod">Click here</a> '+
									'if you would like to reserve this Pod now.</p>'

                                    $('.quoteAccContent').eq(3).children('.quoteAccInner').html($qteNfo);

                                    sec3He=$('.quoteAccContent').eq(3).children('.quoteAccInner').height()+20;
                                    $('.quoteAccContent').animate({
                                        height:'0px'
                                    },{
                                        duration:300,
                                        queue:false
                                    });
                                    $('.quoteAccContent').eq(3).animate({
                                        height:sec3He+'px'
                                        },{
                                        duration:300,
                                        queue:false
                                    });
									
									$('.reservePod').click(function(){
										$.ajax({ 
										  type: "POST", 
										  url: "/media/scripts/php/process.php", 
										  data: "func=requestPOD", 
										  success: function(data){ 
										  	$('.requestPod').text(data);
										  } 
										});
										return false;								
									});

                                }
                                else{
                                    alert(data);
                                    $('.qNfo .ThnkYou')
									.html("Unfortunately an error occurred whistled trying to process your request.");
                                    $().refreshSum();

                                }
                            }
                        });
                    }
                    else{
                        $('.qNfo .ThnkYou').html("Incorrect value for maths question.");
                        $().refreshSum();
                    }
                }
            });

        }
        else{
            $('.qNfo .ThnkYou').html("<strong>Please ensure all fields have been filled in correctly.</strong>");
            $().refreshSum();
        }
        aniSec=false;
        return false;
    }

    if(aniSec){
        accHeight=$('.quoteAccContent').eq(accIndx).children('.quoteAccInner').height()+10;
        $('.quoteAccContent').animate({
            height:0
        },{
            duration:600,
            queue:false,
            'easing':'easeInOutExpo'
        }).eq(accIndx)
        .animate({
            height:accHeight
        },{
            duration:600,
            queue:false,
            'easing':'easeInOutExpo'
        });
    }
    return false;
    });


/* SPECIAL OFFERS */

//load offers
$.ajax({
    type: "POST",
    url: "/media/scripts/php/process.php",
    data: "func=returnOffers",
    success:function(data){
        $('.offerLst').html(data)

        thsNme=$('.soC');
        thsNme.animate({
            right:0
        },{
            duration:300,
            queue:false
        });
        $('.soBut').css({
            backgroundPosition:'right center'
        });

        soCsi=setInterval(function(){
            clearInterval(soCsi);
            thsNme=$('.soC');
            secWid=thsNme.width()-20;
            secWid="-"+secWid+"px";
            thsNme.animate({
                right:secWid
            },{
                duration:300,
                queue:false
            });
            $('.soBut').css({
                backgroundPosition:'right center'
            });
        },9000);


        ofLst=$('.offerLst li');
        ofLstCount=ofLst.size()-1;
        ofLst.css({
            left:170,
            display:'block'
        }).eq(0).css({
            left:0,
            display:'block'
        });
        $().animateOffers();

        $('.mOffers').click(function(){
            thsOb=$(this);
            thsOb.animate({
                opacity:0
            },300,function(){
                thsOb.css({
                    display:'none'
                });
            })
            clearInterval(spOffInt)
            ofLst.each(function(){
                thsIndx=$(this).parent().children('li').index(this);
                thsLft=$(this).css('left');
                if(thsLft=='0px'){
                    if(thsIndx==ofLstCount){
                        aniNxt=0;
                        aniOut=thsIndx;
                    }else{
                        aniNxt=thsIndx+1;
                        aniOut=thsIndx;
                    }
                }
            });
        ofLst.eq(aniOut).animate({
            left:'-170px'
        },500,function(){
            $(this).css({
                left:170
            });
            $().animateOffers();
            thsOb.css({
                display:'block'
            }).animate({
                opacity:1
            },{
                duration:500,
                queue:false
            });
        });
        ofLst.eq(aniNxt).animate({
            left:0
        },{
            duration:500,
            queue:false
        });
                                        
        return false;
        })

    $('.soBut').click(function(){
        thsPar=$(this).parent();
        secRte=thsPar.css('right');
        secWid=thsPar.width()-20;
        secWid="-"+secWid+"px";
        if(secRte==secWid){
            thsPar.animate({
                right:0
            },{
                duration:300,
                queue:false
            });
            $(this)
            .css({
                backgroundPosition:'left center'
            });
        }

        else{
            thsPar.animate({
                right:secWid
            },{
                duration:300,
                queue:false
            });
            $(this)
            .css({
                backgroundPosition:'right center'
            });
        }
    });

    $('.soC').mouseout(function(){
        thsNme=$(this);
        clearInterval(soCsi);
        soCsi=setInterval(function(){
            clearInterval(soCsi);
            secWid=thsNme.width()-20;
            secWid="-"+secWid+"px";
            thsNme.animate({
                right:secWid
            },{
                duration:300,
                queue:false
            });
            $('.soBut').css({
                backgroundPosition:'right center'
            });
        },2000);
    }).
    mouseover(function(){
        clearInterval(soCsi);
    });

}
});


});


//title tag remove/replace
$('a, img').mouseover(function(){
    if($(this).attr('title')!=undefined){
        titleStr=$(this).attr('title');
        $(this).removeAttr('title');
    }
}).mouseout(function(){
    if(titleStr!=""){
        $(this).attr('title',titleStr);
        titleStr="";
    }
});

$('a').click(function(){
    thisLink=$(this).attr('href');
    thisRel=$(this).attr('rel');
    if(thisLink!="" && thisLink!=" " && thisLink!="#")
    {
        if(thisRel=="nw"){
            window.open(thisLink);
        }
        else{
            window.location=thisLink;
        }
    }
    return false;
});

function CurrentDocName()
{
    thisURL=document.location.href;
    strUrl=thisURL.split('/');
    strUrl=strUrl.pop().split('?');
    strUrl=strUrl[0].split('#');
    return strUrl[0];
}

function CheckEmail(inputemail) {
    AtPos = inputemail.indexOf("@");
    StopPos = inputemail.lastIndexOf(".");
    if (AtPos == -1 || StopPos == -1){
        return false;
    }
    else{
        return true;
    }
}

});

(function($) {
    $.fn.extend({
        returnSzeOpts:function(inpIndx){
            $('.podSc .quSizeOpts').html("Loading Pods");
            $.ajax({
                type: "POST",
                url: "/media/scripts/php/process.php",
                data: "func=returnSizeOpts&sizeid="+inpIndx,
                success: function(data){
                    oupData=data.split("{spl}");
                    $('.podSc .quSizeOpts').html(oupData[0]);
                    $('.secLft .quRecLen').html(oupData[1]);
                    $('.secLft .qRecNfo').html(oupData[2]);
                    $('.secRte').css({backgroundPosition:'0px -'+oupData[3]+'px'});

                    $('.quSizeOpts input[type="radio"]').click(function(){
                        bgVal=$('.quSizeOpts input[type="radio"]:checked').val();
                        $.ajax({
                            type: "POST",
                            url: "/media/scripts/php/process.php",
                            data: "func=reBgPos&bgIndx="+bgVal,
                            success: function(data){
                                $('.secRte').css({backgroundPosition:'0px -'+data+'px'});

                               //re-set heights
                                accHeight=$('.quoteAccContent').eq(0).children('.quoteAccInner').height()+10;
                                $('.quoteAccContent').eq(0).animate({height:accHeight},{duration:300,queue:false });
                            }
                        });

                    });

                    //re-set heights
                    accHeight=$('.quoteAccContent').eq(0).children('.quoteAccInner').height()+10;
                    $('.quoteAccContent').eq(0).animate({height:accHeight},{duration:300,queue:false });
                }
            });
        },
        frmImps:function(options){
            return this.each(function() {
                var thsName=$(this);
                thsName.focus(function(){
                    thsVal=thsName.val();
                    thsName.val("");
                }).blur(function(){
                    if(thsName.val()==""){
                        thsName.val(thsVal);
                    }
                });
            });
    },
    refreshSum:function(){
        elm=$('.qNfo .qSum');
        elm.html("refreshing");
        $.ajax({
            type: "POST",
            url: "/media/scripts/php/process.php",
            data: "func=returnMath",
            success: function(data){
                elm.html(data);
            }
        });
    },
    returnPodHeadSelectNfo:function(){
        pdSzeItmId=$('.quoteAccInner .podSc select').val()+"{spl}"+$('.quoteAccInner .podSc input[type="radio"]:checked').val()
        theObj=$(this);
        $.ajax({
            type: "POST",
            url: "/media/scripts/php/process.php",
            data: "func=returnPodHeadSelectNfo&pdSzeItmId="+pdSzeItmId,
            success: function(data){
                theObj.html(data);
            }
        });
    },
    returnDteHeadSec:function(){
        theObj=$(this);
        pstDteVal=[];
        $('.quoteAccInner .dteDur select').each(function(){
            thsVal=$(this).val();
            pstDteVal.push(thsVal);
        });
        $.ajax({
            type: "POST",
            url: "/media/scripts/php/process.php",
            data: "func=returnDteHeadSec&sndNfo="+pstDteVal.toString(),
            success: function(data){
                theObj.html(data);
            }
        });
    },
    animateOffers:function(){
        spOffInt=setInterval(function(){
            ofLst.each(function(){
                thsIndx=$(this).parent().children('li').index(this);
                thsLft=$(this).css('left');
                if(thsLft=='0px'){
                    if(thsIndx==ofLstCount){
                        aniNxt=0;
                        aniOut=thsIndx;
                    }else{
                        aniNxt=thsIndx+1;
                        aniOut=thsIndx;
                    }
                }
            });
        ofLst.eq(aniOut).animate({
            left:'-170px'
        },600,function(){
            $(this).css({
                left:170
            })
            });
        ofLst.eq(aniNxt).animate({
            left:0
        },{
            duration:600,
            queue:false
        });
        },4000);
    }
});
})(jQuery);
