var boxopenedid = "";
var boxexpanding = false;

var normalboxwidth = "81px";
var normalboxheight = "77px";

var hoveredboxwidth = "87px";
var hoveredboxheight = "83px";

var expandedboxheight = "700px";
var newsexpandedboxheight = "900px"; //1130px
var animationspeed = 500;
var fastanimationspeed = 10;

var imagepath = "/images/small/";
var imageextension = ".png";

//Box Heights
var boxheights = new Array();
boxheights["box1"]="620px";
boxheights["box2"]="620px";
boxheights["box3"]="620px";
boxheights["box4"]="620px";
boxheights["news"]="620px";

//pictureleft positions
var pictureleft = new Array();
pictureleft["home"] = "430px";
pictureleft["box1"] = "670px";
pictureleft["box2"] = "690px";
pictureleft["box3"] = "790px";
pictureleft["box4"] = "670px";
pictureleft["news"] = "670px";


//Pictures
var pictures = new Array();
pictures[0] = "home";
pictures[1] = "news";
pictures[2] = "box2";
pictures[3] = "box1";
pictures[4] = "box4";
pictures[5] = "box3";


//Home page images
var currentpictureindex = 0;
//Process images
var currentpic1index = 1; var currentpic2index = 1;
var pic1count = 4; var pic2count = 7;
//News Items
var currentnewsindex = 1; var maxnewsitem = 2;


function getArrayIndex(arr,value) {
    var arrposition=-1
    for (i=0;i<arr.length;i++) {
        if(arr[i]===value) {
            arrposition = i;
            break;
        }
    }
    return arrposition;
}

function showpicture(){
    var imagename = pictures[currentpictureindex];
    var img = "<img src=\"/images/small/" + imagename + ".png\" onmouseover=\"TJPzoom(this, '/images/large/" + imagename + ".jpg');\">";    
    $("#homepagerightimagecontent").fadeOut("slow",
        function(){            
            $("#homepagerightimagecontent").html(img);
            $("#homepagerightimagecontent").fadeIn("slow");
        }
    );    
}

function shownews(op, np) {
    $("#np" + op).fadeOut(fastanimationspeed,
        function() {
            $("#np" + op).hide();
            $("#np" + np).fadeIn(animationspeed);
        }
    );
}

function showclickedpicture(){    
    if (boxopenedid.length>0){
        var imageindex = getArrayIndex(pictures,boxopenedid);
        currentpictureindex=imageindex;
        showpicture();
    }
    resetwithoutpicture();
}

function resetwithoutpicture()
{
    $(".box").animate({
        width: normalboxwidth,
        height: normalboxheight
    }, animationspeed);

    $(".boxinfo").hide();
    boxopenedid="";
    
    //Hide the images
    $("#homepageleftimage").hide();
    $("#homepagerightimage").show();
    $("#homepagerightimagecontent").show();
    $("#rightimagecontrols").show();
    $("#homepagetext").show();
    $(".closebutton").hide();
    $(".box").removeClass("nocursorpointer");
    
    $("#homepagerightimage").animate({
    left: "430px"
    }, animationspeed);
}

function reset()
{
    $(".box").animate({
        width: normalboxwidth,
        height: normalboxheight
    }, animationspeed);

    $(".boxinfo").hide();
    boxopenedid="";
    
    //Hide the images
    currentpictureindex=0;
    $("#homepageleftimage").hide();
    $("#homepagerightimage").show();
    $("#homepagerightimagecontent").show();
    $("#rightimagecontrols").show();
    $("#homepagetext").show();
    $(".closebutton").hide();
    $(".box").removeClass("nocursorpointer");


    $("#homepagerightimage").fadeOut(fastanimationspeed,
        function() {
            $("#homepagerightimage").animate({
                left: "430px"
            }, animationspeed,
                function() {
                    $("#homepagerightimage").fadeIn("slow");
                }
            );
        }
    );
    $(".boxinfo").html("");
    //$("#wall").html("");
    showpicture();
}

function initialise()
{
    //Initialise Contents
    $("#boxbottominfo").html($("#infobottombox1").html());
    $("#contactusboxcontent").html($("#infocontactusbox1").html());
    $("#contactusfootercontent").html($("#contactusfootercontent2").html());

    //Underlines the selected links
    $("#bottombox1").addClass("nounderline");$("#bottombox2").addClass("underline");
    $("#contactusbox1").addClass("nounderline");$(".contactusboxlink:not(.nounderline)").addClass("underline");
    $(".newstoplink").addClass("underline");
    

    //Add the greater than sign
    $("#bottombox1").html($("#bottombox1").html() + "<span id='gt'> <</span>");
    $("#contactusbox1").html($("#contactusbox1").html() + "<span id='gtc'> <</span>");    
}

function resetrightimageleftposition(){
    if (boxopenedid.length>0)
    {
        if (boxopenedid==="box1" || boxopenedid==="box2" || boxopenedid==="box3"){
            $("#homepagerightimage").fadeOut(fastanimationspeed,
                function(){
                    $("#homepagerightimage").animate({
                        left: pictureleft[boxopenedid]
                    }, animationspeed,
                        function()
                        {
                            $("#homepagerightimage").fadeIn(animationspeed);
                        }
                    );
                }
            );
        }
    }
}

function resetleftimageleftposition(){
    if (boxopenedid.length>0)
    {
        if (boxopenedid==="box4" || boxopenedid==="news"){
            $("#homepageleftimage").fadeOut(fastanimationspeed,
                function(){
                    $("#homepageleftimage").animate({
                        left: "0px"
                    }, animationspeed,
                        function()
                        {
                            $("#homepageleftimage").fadeIn(animationspeed);
                        }
                    );
                }
            );
        }
    }
}

function getServiceResults(methodname, methoddata, successfunction){
    jQuery.ajax({
        type: "POST",
        url: "contact.asmx/" + methodname,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: methoddata,
        error: function (xhr, desc, exceptionobj) {
            alert("An error occurred.");
        },
        success: function(data){
				successfunction(data);
		}
    });
}

function showresponse(where,data){
    if (data==="yes")
    {
        var msg = "Thank you. Your message has been sent.<br>You will receive confirmation by email shortly.";
        if (where === "contactuserrormessagetext") {
            msg = "<span style='background-color:transparent;color:white;'>" + msg + "</span>";
        }
        else {
            msg = "<span style='background-color:white;color:#808080;'>" + msg + "</span>";
        }
        $("#" + where).html(msg);
        if (where === "contactuserrormessagetext"){adjustcontactusboxheight();}
        jQuery().oneTime(5000, 
            function(i){
                reset();
                //Close header box
                $("#headerinfoboxcontainer, #errormessage").fadeOut("slow");
            }
        );
    }else{                
        var topmsg = data;
        var bottommsg = "<div id='contacterrorbox'>" + data + "</div>";
        if(where==="contactuserrormessagetext"){$("#"+where).css("display","block");$("#"+where).html(bottommsg);adjustcontactusboxheight();}
        else{$("#"+where).css("display","block");$("#"+where).html(topmsg);}
    }
}

function submitemailenquiry(){
    var title = $("#emailenquiry_title").val();
    var firstname = $("#emailenquiry_firstname").val();
    var lastname = $("#emailenquiry_surname").val();
    var town = $("#emailenquiry_town").val();
    var country = $("#emailenquiry_country").val();
    var emailaddress = $("#emailenquiry_email").val();
    var telephone = $("#emailenquiry_telehphone").val();
    var subject = $("#emailenquiry_subject").val();
    var enquiry = $("#emailenquiry_enquiry").val();
    var hearaboutus = $("#emailenquiry_hearaboutus").val();
    var joinmailinglist = $("#emailenquiry_mailinglist").attr("checked");

    var values = "{title:'" + title + "',firstname:'" + firstname + "',lastname:'" + lastname + "',town:'" + town + "',country:'" + country + "',emailaddress:'" + emailaddress + "',telephone:'" + telephone + "',subject:'" + subject + "',enquiry:'" + enquiry + "',hearaboutus:'" + hearaboutus + "',joinmailinglist:'" + joinmailinglist + "'}";

    $("#contactuserrormessagetext").show(); $("#contactuserrormessagetext").html("<span style='color:white'>Sending...</span>");
    adjustcontactusboxheight();
    getServiceResults("emailenquiry",values,function(data){showresponse("contactuserrormessagetext",data.d);})        
}

function submitrequestabrochure(){
    var title = $("#requestabrochure_title").val();
    var firstname = $("#requestabrochure_firstname").val();
    var lastname = $("#requestabrochure_surname").val();
    var town = $("#requestabrochure_town").val();
    var country = $("#requestabrochure_country").val();
    var emailaddress = $("#requestabrochure_email").val();
    var telephone = $("#requestabrochure_contactnumber").val();
    var requestabrochure_interest_baby = $("#requestabrochure_interest_baby").attr("checked");
    var requestabrochure_interest_family = $("#requestabrochure_interest_family").attr("checked");
    var requestabrochure_interest_portrait = $("#requestabrochure_interest_portrait").attr("checked");
    var hearaboutus = $("#requestabrochure_hearaboutus").val();
    var joinmailinglist = $("#requestabrochure_mailinglist").attr("checked");

    var values = "{title:'" + title + "',firstname:'" + firstname + "',lastname:'" + lastname + "',town:'" + town + "',country:'" + country + "',emailaddress:'" + emailaddress + "',telephone:'" + telephone + "',requestabrochure_interest_baby:'" + requestabrochure_interest_baby + "',requestabrochure_interest_family:'" + requestabrochure_interest_family + "',requestabrochure_interest_portrait:'" + requestabrochure_interest_portrait + "',hearaboutus:'" + hearaboutus + "',joinmailinglist:'" + joinmailinglist + "'}";

    $("#contactuserrormessagetext").show(); $("#contactuserrormessagetext").html("<span style='color:white'>Sending...</span>");
    adjustcontactusboxheight();
    getServiceResults("requestabrochure",values,function(data){showresponse("contactuserrormessagetext",data.d);})      
}

function submitrequestacallback(){
    var title = $("#requestacallback_title").val();
    var firstname= $("#requestacallback_firstname").val();
    var lastname = $("#requestacallback_surname").val();
    var emailaddress = $("#requestacallback_email").val();
    var telephone = $("#requestacallback_phonenumber").val();
    var enquiry = $("#requestacallback_enquiry").val();
    var joinmailinglist = $("#requestacallback_mailinglist").attr("checked");   //attr("checked");

    var values = "{title:'" + title + "',firstname:'" + firstname + "',lastname:'" + lastname + "',emailaddress:'" + emailaddress + "',telephone:'" + telephone + "',enquiry:'" + enquiry + "',joinmailinglist:'" + joinmailinglist + "'}"

    $("#contactuserrormessagetext").show(); $("#contactuserrormessagetext").html("<span style='color:white'>Sending...</span>");
    adjustcontactusboxheight();
    getServiceResults("requestcallback",values,function(data){showresponse("contactuserrormessagetext",data.d);})
}

function submitjoinourmailinglist(){
    var title = $("#joinourmailinglist_title").val();
    var firstname= $("#joinourmailinglist_firstname").val();
    var lastname = $("#joinourmailinglist_surname").val();
    var emailaddress = $("#joinourmailinglist_email").val();
    var hearaboutus = $("#joinourmailinglist_hearaboutus").val();
    var enquiry = $("#joinourmailinglist_enquiry").val();
    var joinmailinglist = $("#joinourmailinglist_enquiry").attr("checked");

    var values = "{title:'" + title + "',firstname:'" + firstname + "',lastname:'" + lastname + "',emailaddress:'" + emailaddress + "',hearaboutus:'" + hearaboutus + "',enquiry:'" + enquiry + "',joinmailinglist:'" + joinmailinglist + "'}"


    $("#contactuserrormessagetext").show(); $("#contactuserrormessagetext").html("<span style='color:white'>Sending...</span>");
    adjustcontactusboxheight();
    getServiceResults("mailinglist",values,function(data){showresponse("contactuserrormessagetext",data.d);})
}

function submittopbox()
{
    var x = $("#inf").css("display");  // id for the checkbox
    var name = $("#topboxB_getbackname").val(); 
    var emailaddress = $("#topboxB_getbackemail").val();
    var telephone = $("#topboxB_getbackphone").val();
    var nameA = $("#topboxA_mailingcontactname").val();
    var emailaddressA = $("#topboxA_mailingcontactemail").val();    
    var joinmailinglist = $("#checkboxcolour").attr("checked");
    
    var values="{}";
    if(x==="block")  //check if the checkbox is visible(or defined any of the 2 forms in topboxA & topboxB )?
    {
        //if yes, 'onclicking submit button' we should get the values from topboxB where the checkbox is part of the form(visible)
        values = "{name:'" + name + "',emailaddress:'" + emailaddress + "',telephone:'" + telephone + "',joinmailinglist:'" + joinmailinglist + "'}";

        $("#errormessage").show(); $("#errormessage").html("<span style='color:#808080'>Sending...</span>");        
        getServiceResults("requestcall",values,function(data){showresponse("errormessage",data.d);})
    }
    else    //if not, onclick we get the values from topboxA where the checkbox is not defined in the form
    {
        values = "{name:'" + nameA + "',emailaddress:'" + emailaddressA + "'}";

        $("#errormessage").show(); $("#errormessage").html("<span style='color:#808080'>Sending...</span>");
        getServiceResults("enewsletter",values,function(data){showresponse("errormessage",data.d);}) 
    }
}            

//Clearing the preset values for the text boxes in the topheader forms (onclicking anywhere on the textbox)
function setupheaderbox()
{
    $("#headerinfoboxcontent").find("#topboxB_getbackname").click(
        function(){
            var value = $("#topboxB_getbackname").val();
            if (value==="name"){
                $(this).val("");
            }
        }
    );
    
    $("#headerinfoboxcontent").find("#topboxB_getbackemail").click(
        function(){
            var value = $("#topboxB_getbackemail").val();
            if (value==="email address"){
                $(this).val("");
            }
        }
    );
    
    $("#headerinfoboxcontent").find("#topboxB_getbackphone").click(
        function(){
            var value = $("#topboxB_getbackphone").val();
            if (value==="telephone number"){
                $(this).val("");
            }
        }
    ); 
     
    $("#headerinfoboxcontent").find("#topboxA_mailingcontactname").click(
        function(){
            var value = $("#topboxA_mailingcontactname").val();
            if (value==="name"){
                $(this).val("");
            }
        }
    );    
    
    $("#headerinfoboxcontent").find("#topboxA_mailingcontactemail").click(
        function(){
            var value = $("#topboxA_mailingcontactemail").val();
            if (value==="email address"){
                $(this).val("");
            }
        }
    );        
}


$(document).ready(function() {
    //Header Boxes
    $(".headerbox").click(function() {
        var clickedboxid = $(this).attr("id");
        if (clickedboxid === "headerbox1") {
            $("#inf").show();
        }
        else {
            $("#inf").hide();
        }
        $("#headerinfoboxcontainer").fadeIn("fast", function() {
            $("#headerinfoboxcontent").html($("#info" + clickedboxid).html());
            reset();
            setupheaderbox();
        });
    });


    //Close Boxes
    $("#closebutton").click(function() {
        $("#headerinfoboxcontainer, #errormessage").fadeOut("slow");
        $("#errormessage").html("");
    });



    //Middle Boxes
    $(".box").mouseenter(function() {
        var bw = $(this).width() + "px";
        var bh = $(this).height() + "px";
        if (normalboxheight === bh && normalboxwidth === bw) {
            $(this).animate({
                width: hoveredboxwidth,
                height: hoveredboxheight
            }, fastanimationspeed);
        }
    });

    $(".box").mouseleave(function() {
        var currentboxwidth = $(this).width();
        var boxid = $(this).attr("id");
        if (currentboxwidth < 100 && boxid != "news") {
            $(this).animate({
                width: normalboxwidth,
                height: normalboxheight
            }, fastanimationspeed);
        }
        else if (boxid === "news" && currentboxwidth < 100 && (boxexpanding === false)) {
            $(this).animate({
                width: normalboxwidth,
                height: normalboxheight
            }, fastanimationspeed);
        }
    });

    $(".closebutton").click(function() {
        boxopenedid = "";
        reset();
    });

    $(".box").click(function() {
        //Close header box
        $("#headerinfoboxcontainer, #errormessage").fadeOut("slow");

        var clickedboxid = $(this).attr("id");
        //Check if the box is expanded
        var bh = $(this).height();
        var bw = $(this).width();
        var newboxdisplay = $("#newstoplinks").css("display");
        if (bw > 100 && clickedboxid === "news" && newboxdisplay === "block" && boxopenedid.length == 0) {
            //Close the box if it is opened
            $("#" + boxopenedid).animate({
                width: normalboxwidth,
                height: normalboxheight
            }, animationspeed);
            $("#" + boxopenedid).find(".boxinfo").css("display", "none");
        }
        else if (bw > 100 && clickedboxid === "news") {
            $(this).animate({
                width: "580px"
            }, animationspeed);
        }
        else if (bh > 100) {

        }
        else {
            //Hide the images
            $("#homepagerightimagecontent").hide();
            $("#rightimagecontrols").hide();
            $("#homepageleftimage").hide();
            $("#homepagerightimage").hide();
            $("#homepagetext").hide();


            //Close the box if it is opened
            if (boxopenedid.length > 0) {
                $("#" + boxopenedid).animate({
                    width: normalboxwidth,
                    height: normalboxheight
                }, animationspeed);
                $("#" + boxopenedid).find(".boxinfo").css("display", "none");
                $("#" + boxopenedid).find(".boxinfo").html("");
            }

            //Display Content
            initialise();
            $(".boxinfo").html("");
            $(this).find(".boxinfo").fadeIn(750,
                    function() {
                        //$("#wall").html("");
                        $(this).html($("#info" + clickedboxid).html());
                        $(this).find("img").fadeIn("slow");
                    }
                );

            currentpictureindex = getArrayIndex(pictures, clickedboxid);

            var img = "<img src='images/small/" + clickedboxid + ".png' />";
            var imageboxname = "#homepagerightimagecontent";
            if (clickedboxid === "box4" || clickedboxid === "news") {
                imageboxname = "#homepageleftimagecontent";
            }
            if (clickedboxid === "news") {
                boxexpanding = true;
                $(this).animate({
                    width: "580px",
                    height: "77px"
                }, animationspeed, function() { boxexpanding = false; });
                $("#newsboxcontent").hide();
            }
            else {
                $(this).animate({
                    width: "590px",
                    height: boxheights[clickedboxid]
                }, animationspeed);
            }
            $(".box").removeClass("nocursorpointer");
            $(this).addClass("nocursorpointer");
            if (imageboxname === "#homepagerightimagecontent") {
                $("#homepagerightimage").show();
            }
            $(imageboxname).fadeIn(animationspeed, function() {
                $(imageboxname).html(img);
            });

            //Close Buttons
            //Hide all the close buttons
            //Show the current box's close button
            $(".closebutton").css("display", "none");
            $(this).find(".closebutton").css("display", "block");
            boxopenedid = $(this).attr("id");

            //Reset right image x coordinate
            resetrightimageleftposition();
            resetleftimageleftposition();
        }
    });
    //Header Logo
    $("#header").click(
        function() {
            reset();
        }
    );

    //Picture Left and Next    
    $("#lnkprevimg").click(
        function() {
            if (currentpictureindex >= 1) {
                currentpictureindex -= 1;
            }
            else {
                currentpictureindex = 5;
            }
            showpicture();
        }
    );

    $("#lnknextimg").click(
        function() {
            if (currentpictureindex <= 4) {
                currentpictureindex += 1;
            }
            else {
                currentpictureindex = 0;
            }
            showpicture();
        }
    );


    //Image clicks    
    $("#homepageleftimage").click(
        function() {
            showclickedpicture();
        }
    );
    $("#homepagerightimagecontent").click(
        function() {
            showclickedpicture();
        }
    );

    $("#homepagerightimage, #homepageleftimage").tooltip({
        track: true,
        showBody: " - ",
        fixPNG: true,
        opacity: 1,
        top: -100,
        left: -40,
        bodyHandler: function() {
            output = "";
            if (boxopenedid.length > 0) {
                output = "<img align='left' src='/images/view-in-more-detail.png' />";
            }
            return output;
        },
        showURL: false
    });

    var boxbottomimageextextension = ".jpg";

    jQuery().everyTime(10000, function(i) {
        $("#boxbottominfo").find("#infobottombox1img").fadeOut(animationspeed,
            function() {
                $("#infobottombox1img").attr("src", "/images/process/infobottombox1img" + currentpic1index + ".jpg");
                $("#infobottombox1img").fadeIn("slow");
            }
        );
        if (currentpic1index >= pic1count) { currentpic1index = 1; } else { currentpic1index++; }
    });
    jQuery().everyTime(5000, function(i) {
        $("#boxbottominfo").find("#infobottombox2img").fadeOut(animationspeed,
            function() {
                $("#infobottombox2img").attr("src", "/images/process/infobottombox2img" + currentpic2index + boxbottomimageextextension);
                $("#infobottombox2img").fadeIn("slow");
            }
        );
        if (currentpic2index >= pic2count) { currentpic2index = 1; } else { currentpic2index++; }
    });
    initialise();
});