$(document).ready(function() {
    if (window.XMLHttpRequest == 'function(){return new ActiveXObject("Microsoft.XMLHTTP")}') {
        $("#menu li").hover(
			function() { $("ul", this).show("fast"); },
			function() { }
		);
    } else {
        $("#menu li ul").fadeTo("normal", 0);
        $("#menu li").hover(
			function() { $("ul", this).css("display", "block"); $("ul", this).fadeTo(400, 1); },
			function() { $("ul", this).fadeTo(1, 0); }
		);
    }
    if (document.all) {
        $("#menu li").hoverClass("hover");
    }

    if (document.getElementById("menu")) {
        //adds last item class to the last li in the first level menu
        var elements = $("div.menu ul").children(".first-item");
        var assigned = false;
        var i = elements[0];
        while (assigned == false) {
            if ($(i).next().length > 0) {
                i = $(i).next();
            }
            else {
                assigned = true;
                $(i).addClass("last-item");
            }
        }
    }

    if (document.getElementById("default") || document.getElementById("threecol")) {
        $("div.side-col h2").html($("div.side-col h2").html().replace(/-/g, " "));
    }

    //change the info-container showing
    $("ul.tabs li a").click(
		function() {
		    var href = $(this).attr("href").replace("#", "").replace(window.location, "");

		    $("ul.tabs li").each(
				function(i) {
				    $(this).removeClass("selected");
				}
			);

		    $(this).parents("li").addClass("selected");

		    $("div.info-container").each(
				function(i) {
				    if ($(this).attr("id") == href) {
				        $(this).removeClass("hide").addClass("showing");
				    }
				    else {
				        $(this).addClass("hide").removeClass("showing");
				        $(this).children(".column-two").children().each(
							function(i) {
							    if ($(this).attr("class").match("slide-one")) {
							        $(this).removeClass("hide");
							    }
							    else {
							        $(this).addClass("hide");
							    }
							}
						)
				    }
				}
			);
		    sIFR.replaceElement(".case-studies .showing h4", named({ sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#FF9900", sCase: "upper", sWmode: "transparent" }));
		    sIFR.replaceElement(".case-studies .showing .column-one span", named({ sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#4b4b4b", sHoverColor: "#FF9900", sWmode: "transparent" }));
		    return false;
		}
	);
    //change the slide within the info container
    $("div.info-container div.buttons a").click(
		function(i) {
		    var href = $(this).attr("href").replace("#", "").replace(window.location, "");

		    $(this).parents(".column-two").children().each(
				function(i) {
				    if ($(this).attr("class").match(href)) {
				        $(this).removeClass("hide");
				    }
				    else {
				        $(this).addClass("hide");
				    }
				}
			);
		    return false;
		}
	);
	//for email change(18/0282008)
    $("div.slider-scope").children(".slider").width(180);
    $("div.slider-scope").children(".slide-twor").width(180);
    //content slider
    $("div.slider-scope").each(
		function(i) {
		    //to make sure they all have position relative and overflow hidden
		    $(this).css("position", "relative").css("overflow", "hidden");
		}
	);
    //make a clone of the slider content to have cyclic animation

    $("div.slider").each(
		function(i) {
		    //var height = this.offsetHeight;

		    $(this).css("position", "absolute").css("top", "0px").css("height", $(this).height() + "px");

		}
	);
    $("div.slider").each(
		function(i) {
		    $(this).parent().append($(this).clone().attr("class", "slider-two").css("top", $(this).height()).css("clear", "both"));
		}
	)
    $("div.slider-scope").hover(
		function() {
		    $(".slider").stop();
		    $(".slider-two").stop();
		},
		function() {
		    //first over hover
		    //get the difference between 
		    var currtop = parseInt($(this).children(".slider").css("top"));
		    var newtop = (parseInt($(this).children(".slider").css("height"))) * -1;
		    var diff = currtop - newtop;

		    var time = (diff / 20) * 1000;

		    var currtop2 = parseInt($(this).children(".slider-two").css("top"));
		    var newtop2 = (parseInt($(this).children(".slider-two").css("height"))) * -1;
		    var diff2 = currtop2 - newtop2;

		    var time2 = (diff2 / 20) * 1000;

		    $(this).children(".slider").animate({ top: newtop }, time, "linear", function() { el = $(".slider"); callBack(el); });
		    $(this).children(".slider-two").animate({ top: newtop }, time2, "linear", function() { el = $(".slider-two"); callBack(el); });


		}

	);
    var aaa = function() {
        //first over hover
        //get the difference between 
        var currtop = parseInt($("div.slider-scope").children(".slider").css("top"));
        var newtop = (parseInt($("div.slider-scope").children(".slider").css("height"))) * -1;
        var diff = currtop - newtop;

        var time = (diff / 20) * 1000;

        var currtop2 = parseInt($("div.slider-scope").children(".slider-two").css("top"));
        var newtop2 = (parseInt($("div.slider-scope").children(".slider-two").css("height"))) * -1;
        var diff2 = currtop2 - newtop2;

        var time2 = (diff2 / 20) * 1000;

        $("div.slider-scope").children(".slider").animate({ top: newtop }, time, "linear", function() { el = $(".slider"); callBack(el); });
        $("div.slider-scope").children(".slider-two").animate({ top: newtop }, time2, "linear", function() { el = $(".slider-two"); callBack(el); });


    }
    aaa();

    function callBack(element) {

        //set the element back to the other animating element
        var height = parseInt($(element).css("height"));
        $(element).css("top", height + "px");

        var currtop = parseInt($(element).css("top"));
        var newtop = (parseInt($(element).css("height"))) * -1;
        var diff = currtop - newtop;

        var time = (diff / 20) * 1000;
        //start the animating again
        $(element).animate({ top: newtop }, time, "linear", function() { el = $(element); callBack(el); });
    }



    //swf inserts
    if (document.getElementById("flash")) {

        var so = new SWFObject("/media/flash/get_started.swf", "sotester", "225", "106", "6", "#000000");
        so.addParam("wmode", "transparent");
        so.write("flash");
    }

    if (document.getElementById("homeFlash")) {
        var so = new SWFObject("/media/flash/rotator.swf", "sotester", "225", "428", "6", "#ffffff");
        so.addParam("wmode", "transparent");
        so.write("homeFlash");
    }

    if (document.getElementById("homePhoto")) {
        var so = new SWFObject("/media/flash/rotating_withText.swf", "sotester", "450", "210", "7", "#ffffff");
        so.addParam("wmode", "transparent");
        so.write("homePhoto");
    }

    /*resize sidecol and content*/
    var scHeight = $(".side-col").height();
    var cHeight = $(".content").height();
    if (cHeight > scHeight) {
        $(".side-col").height(cHeight + 4);
    }

    /*offices*/
    $("div.office-locations ul.office-list a").click(
	    function() {
	        var location = $(this).parent().attr("class");
	        $("div.location-info").addClass("offleft");
	        $("div#" + location).removeClass("offleft");

	        return false;
	    }
	);
    $("div.location-info a.link-button").click(
	    function() {
	        $(this).parents(".location-info").addClass("offleft");
	        return false;
	    }
	);

    /*profiles behaviour*/
    /*change location profiles*/

    $(".profile-listing .location-select a.link-button").click(
	    function() {
	        //get the parent id
	        //var id = $(this).parents(".profile-listing").attr("id");
	        //var loc = $("#"+id+" .location-select select option:selected").attr("value");
	        //$("#"+id+" .profiles div.location").removeClass("visible");
	        //$("#"+id+" .profiles div#"+loc).addClass("visible");

	        //sIFR.replaceElement(".profile-listing  .pdf-link span", named({sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#FFFFFF", sHoverColor: "#4B4B4B", sWmode: "transparent"}));
	        //sIFR.replaceElement(".profile-listing  .info h2", named({sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#FFFFFF", sCase: "upper", sWmode: "transparent"}));
	        //sIFR.replaceElement(".profile-listing  .info h3", named({sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#4B4B4B", sCase: "upper", sWmode: "transparent"}));

	        return false;
	    }
	);

    /*change current profile displaying*/

    $(".profile-listing .profiles .ppl-listing a").click(
	    function() {
	        //var id = $(this).parents(".profile-listing").attr("id");
	        //var person = $(this).attr("href").replace("#", "");
	        // var loc = $(this).parents(".location").attr("id");
	        /*reset the li class for the current location*/
	        //$("#"+id+" .profiles #"+loc+" .ppl-listing li").removeClass("selected");
	        //$(this).parent().addClass("selected");
	        /*display the correct profile*/
	        //$("#"+id+" .profiles #"+loc+" .profile-view .profile").removeClass("visible");
	        // $("#"+id+" .profiles #"+loc+" .profile-view #"+person).addClass("visible");

	        //sIFR.replaceElement(".profile-listing .pdf-link span", named({sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#FFFFFF", sHoverColor: "#4B4B4B", sWmode: "transparent"}));
	        //sIFR.replaceElement(".profile-listing .info h2", named({sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#FFFFFF", sCase: "upper", sWmode: "transparent"}));
	        //sIFR.replaceElement(".profile-listing .info h3", named({sFlashSrc: "/media/sifr/futura-book.swf", sColor: "#4B4B4B", sCase: "upper", sWmode: "transparent"}));


	        return false;
	    }
	);



    /* Main Nav Highlight */
    var curPage = $("div.section h2").html();
    if (curPage != null) {
        curPage = curPage.replace(/and/, '&amp;');
        if (curPage.toLowerCase() == "private clients") {
            curPage = "advice service";
        }

        $("div.header div.menu ul li.submenu a").each(
	        function() {
	            if ($(this).html().toLowerCase() == curPage.toLowerCase()) {
	                $(this).parent().addClass("selected");
	            }
	        }
	    );
    }

    $(".topic-show").css("display", "none");
    $(".topic-hide .more").click(function() {
        $(this).parents(".topic-hide").hide();
        $(this).parents(".topic-hide").next().show(500);
        return false;
    })
    $(".topic-show .less").click(function() {
        $(this).parents(".topic-show").hide();
        $(this).parents(".topic-show").prev().show();
        return false;
    })


    /* Hover state on case studies page */
    $("div.case-studies ul.tabs li a").hover(
	    function() {
	        if ($(this).children("img").hasClass("on")) {
	            $(this).children("img.on").removeClass("offleft");
	            $(this).children("img.off").addClass("offleft");
	        }
	    },
        function() {
            if ($(this).children("img").hasClass("on")) {
                $(this).children("img.on").addClass("offleft");
                $(this).children("img.off").removeClass("offleft");
            }
        }
    );

    $("div.media-scroll div.scroll-pane div.entry a").click(
         function() {
             updateFlashVid($(this).attr("id"), $(this).html(), $(this).attr("type"));
             $(this).parent().parent().find("a").removeClass("selected");
             $(this).addClass("selected");
             //	           updateFlashVid($(this).attr("id"),$(this).attr("type"));
             return false;
         }
	);

    /*
    SWFOBJECT
    Inserting a swf object into the page;
    Requires: swfobject
    */
    if (document.getElementById("mediaPlayer")) {
        type = $("#mediaPlayer").attr("type");

        if (type == "flv") {
            var so = new SWFObject("/media/flash/flv_player.swf", "flvPlayer", "428", "278", "9", "#cccccc");
            //so.addVariable("variable1", "value1");
            path = $("#mediaPlayer").attr("rel");
            so.addVariable("urlPath", path);
            so.write("mediaPlayer");
        }
        else {
            var so = new SWFObject("/media/flash/flv_player_mp3.swf", "flvPlayer", "428", "278", "9", "#cccccc");
            //so.addVariable("variable1", "value1");
            path = $("#mediaPlayer").attr("rel");
            so.addVariable("urlPath", path);
            so.write("mediaPlayer");
        }
    }

    /*JSCROLL*/
    //$('.scroll-pane').jScrollPane({scrollbarWidth: 13, showArrows:true});

    /*************************/
    /*Protosite js ONLY*/
    $("select.q-links").change(
	    function() {
	        if ($(this, "option:selected").attr("value")) {
	            window.location = $(this, "option:selected").attr("value");
	        }
	    }
	);

    /* -------------------------------------------------------------------- 
    NOTE: THIS CODE IS ONLY TO BE USED IN PROTOSITE TO SIMULATE 
    SELECTED STATE, NOT TO BE INCLUDED IN FINAL PRODUCT
    */
    /*
    var curPage = $("div.section h2").html();
    curPage = curPage.replace(/and/, '&amp;');
    if(curPage.toLowerCase() == "private clients") {
    curPage = "advice service";
    }
    $("div.header div.menu ul li.submenu a").each(
    function(){
    if($(this).html().toLowerCase() == curPage.toLowerCase()) {
    $(this).parent().addClass("selected");
    }
    }
    );
    */
    /* ----------------------------- END OF NOTE --------------------------- */

    /* ----------------------------- SHANGHAI MODIFIED ------------------------------ */

    $(".topic-hide .more").click(function() {
        $(this).parents(".topic-hide").hide();
        $(this).parents(".topic-hide").next().show("500", function() {
            changeheight()
        }).addClass("printthis");

        return false;
    })
    $(".topic-show .less").click(function() {
        $(this).parents(".topic-show").hide().removeClass("printthis");
        $(this).parents(".topic-show").prev().show();
        changeheight()
        return false;
    })

    function changeheight() {
        var scHeight = $(".side-col").height();
        var cHeight = $(".content").height();
        $(".side-col").height(cHeight + 4);

    }
    $(".print").click(function() {
        window.print();
    })
    $(".rightMedia:first").css("display", "block");
    $(".media-scroll .entry a").each(function(i, item) {
        $(item).click(function() {
            $(".rightMedia").each(function(t, el) {
                if (t == i) {
                    $(el).css("display", "block")

                }
                else {
                    $(el).css("display", "none")
                }

            });
            changeheight();

        })

    })
    /* ----------------------------- END OF NOTE ------------------------------------ */

});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

//function updateFlashVid(path, title, content)
    function updateFlashVid(path,title,type) {
	if(document.getElementById("mediaPlayer"))
	{
	    if (type == "flv")
	    {
		    var so = new SWFObject("/media/flash/flv_player.swf", "flvPlayer", "428", "278", "9", "#cccccc");
		    //so.addParam("wmode", "transparent");
		    so.addVariable("urlPath", path);					
		    so.write("mediaPlayer");
		}
		else
		{
		    var so = new SWFObject("/media/flash/flv_player_mp3.swf", "flvPlayer", "428", "278", "9", "#cccccc");
		    //so.addVariable("variable1", "value1");		    
		    so.addVariable("urlPath", path);		
		    so.write("mediaPlayer");		    		
		}
	}
	
	/*Protosite js ONLY*/
	$("div.media-scroll div.rightMedia div.subtitle h3").html(title);
}

function enableJSClass()
{
	document.body.className = "jsEnabled";
}
