/*sIFR setup*/
 var helveticaneue = {
  src: '/global/swf/helveticaneue45light.swf'
  ,ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 28, 1.23, 34, 1.22, 38, 1.21, 39, 1.22, 46, 1.21, 50, 1.2, 51, 1.21, 80, 1.2, 83, 1.19, 84, 1.2, 88, 1.19, 89, 1.2, 1.19]
};
 var helveticaneueExtendedLight = {
  src: '/global/swf/helveticaneueExtendedLight.swf'
  ,ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 28, 1.23, 34, 1.22, 38, 1.21, 39, 1.22, 46, 1.21, 50, 1.2, 51, 1.21, 80, 1.2, 83, 1.19, 84, 1.2, 88, 1.19, 89, 1.2, 1.19]
};
 var helveticaneueExtended = {
  src: '/global/swf/helveticaneueExtended.swf'
  ,ratios: [6, 1.41, 9, 1.35, 15, 1.29, 21, 1.25, 22, 1.22, 27, 1.24, 28, 1.23, 32, 1.22, 33, 1.23, 39, 1.22, 51, 1.21, 54, 1.2, 55, 1.21, 91, 1.2, 93, 1.19, 94, 1.2, 98, 1.19, 99, 1.2, 109, 1.19, 110, 1.2, 1.19]
};
 var helveticaneue55roman = {
  src: '/global/swf/helveticaneue55roman.swf'
  ,ratios: [10, 1.32, 16, 1.27, 23, 1.23, 24, 1.21, 25, 1.22, 31, 1.21, 38, 1.2, 42, 1.19, 43, 1.2, 62, 1.19, 66, 1.18, 67, 1.19, 113, 1.18, 114, 1.17, 119, 1.18, 120, 1.17, 1.18]
};
sIFR.activate(helveticaneue);
sIFR.activate(helveticaneueExtendedLight);
sIFR.activate(helveticaneueExtended);
sIFR.activate(helveticaneue55roman);
//sIFR.debug.ratios({ src: '/global/swf/helveticaneue55roman.swf', selector: 'div.hotline span.bold' });
sIFR.replace(helveticaneue, {
	selector: 'div.tab-header-norm h3',
	css: [
        '.sIFR-root {color: #ffffff;}'
      ]
});
/*popup sifrs*/

sIFR.replace(helveticaneueExtended, {
	selector: 'div.popup div.header-box div.head h4',
	css: [
        '.sIFR-root {color: #242424;}'
      ]
});
sIFR.replace(helveticaneueExtended, {
	selector: 'div.popup div.header-box div.body p',
	css: [
        '.sIFR-root {color: #4F5050;}'
      ]
});


/*contact*/
sIFR.replace(helveticaneue, {
	selector: 'div#contact div.tab-header h3',
	css: [
        '.sIFR-root {color: #ffffff;}'
      ]
});
//do the retail sifr's
sIFR.replace(helveticaneue, {
    selector: 'div#retail ul.sidemenu li.selected span.sifr',
    css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #828B4B;}'
        ,'a:hover { color: #828B4B;}'
      ]
});
sIFR.replace(helveticaneue, {
    selector: 'div#retail ul.sidemenu li span.sifr',
    css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #828282;}'
        ,'a:hover { color: #828B4B;}'
      ]
});
sIFR.replace(helveticaneueExtendedLight, {
	selector: 'div#retail h2',
	css: [
        '.sIFR-root {color: #828B4B;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'div#retail p.sifr-p',
	css: [
        '.sIFR-root {color: #748095;}'
      ]
});

//do the commercial sifr's
sIFR.replace(helveticaneue, {
	selector: 'div#commercial ul.sidemenu li.selected li.selected span.sifr',
	css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #77B1BD;}'
        ,'a:hover { color: #77B1BD;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'div#commercial ul.sidemenu li.selected li span.sifr',
	css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #828282;}'
        ,'a:hover { color: #77B1BD;}'
      ]
});
sIFR.replace(helveticaneue, {
    selector: 'div#commercial ul.sidemenu li.selected span.sifr',
    css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #4090A1;}'
        ,'a:hover { color: #4090A1;}'
      ]
});
sIFR.replace(helveticaneue, {
    selector: 'div#commercial ul.sidemenu li span.sifr',
    css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #828282;}'
        ,'a:hover { color: #4090A1;}'
      ]
});
sIFR.replace(helveticaneueExtendedLight, {
	selector: 'div#commercial h2',
	css: [
        '.sIFR-root {color: #4090A1;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'div#commercial div.tab-header h3',
	css: [
        '.sIFR-root {color: #324E6C;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'div#commercial p.sifr-p',
	css: [
        '.sIFR-root {color: #62A3B1;}'
      ]
});

/*default sifrs*/
sIFR.replace(helveticaneue, {
	selector: 'ul.sidemenu li.selected li.selected span.sifr',
	css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #5A86AC;}'
        ,'a:hover { color: #5A86AC;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'ul.sidemenu li.selected li span.sifr',
	css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #828282;}'
        ,'a:hover { color: #5A86AC;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'ul.sidemenu li.selected span.sifr',
	css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #003566;}'
        ,'a:hover { color: #003566;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'ul.sidemenu li span.sifr',
	css: [
        '.sIFR-root {}'
        ,'a { text-decoration: none;}'
        ,'a:link { color: #828282;}'
        ,'a:hover { color: #003566;}'
      ]
});
sIFR.replace(helveticaneueExtendedLight, {
	selector: 'h2',
	css: [
        '.sIFR-root {color: #00457C;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'div.tab-header h3',
	css: [
        '.sIFR-root {color: #ffffff;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'p.sifr-p',
	css: [
        '.sIFR-root {color: #5A86AC;}'
      ]
});
sIFR.replace(helveticaneue55roman, {
	selector: 'div.hotline span.bold',
	css: [
        '.sIFR-root {color: #959595;}'
      ]
});
sIFR.replace(helveticaneue, {
	selector: 'div.hotline span.norm',
	css: [
        '.sIFR-root {color: #ACACAC;}'
      ]
});

$(document).ready(function(){
	
	if(document.getElementById("homeflash"))
	{
		var so = new SWFObject("global/swf/homeflash/homeflash.swf", "flashObj", "941", "360", "9", "#FFFFFF");
		so.addParam("wmode", "transparent");
		//so.addVariable("variable1", "value1");
		so.write("homeflash");
	}
	
	if(document.getElementById("jcarousel"))
    {

	    $('#simpleCarousel').jcarousel({
		   itemVisible: 1,
           itemScroll: 1,
           autoScroll: 0, 
           wrap: true,
           wrapPrev: true
	    });
	    
	    $('#wide-carousel').jcarousel({
		   itemVisible: 1,
           itemScroll: 1,
           autoScroll: 0, 
           wrap: false,
           wrapPrev: false
	    });
	    
	    $('#narrow-carousel').jcarousel({
		   itemVisible: 1,
           itemScroll: 1,
           autoScroll: 0, 
           wrap: false,
           wrapPrev: false,
           afterScrollFunctionCall: changeViewPlansLink
	    });
    }
	
	$("ul.sidemenu li").hover(
	    function()
	    {
	        $(this).addClass("hover");
	    },
	    function()
	    {
	        $(this).removeClass("hover");
	    }
	);
	
	$("input.button").hover(
		function()
		{
			$(this).addClass("hover");
		},
		function()
		{
			$(this).removeClass("hover");
		}
	);
	
	//shadowbox
	links = [];
	$(".shadowbox-flipbook").each(
		function(i)
		{
			/*swap the href and rel attributes*/
			/*swap the href and rel attributes*/
			href = $(this).attr("href");
			rel = $(this).attr("rel");
			$(this).attr("href", rel);
			$(this).attr("rel", href);
			links[i] = this;
		}
	);
	if(links.length > 0)
	{
	    Shadowbox.init({
		    loadingImage: 		'../global/img/shadowbox/loading.gif',
		    overlayBgImage:     '',
		    overlayOpacity:     1
	    });
		Shadowbox.setup(links, {showTitle: true, titleHeight: "20px"});
	}
	links = [];
	$(".shadowbox").each(
		function(i)
		{
			links[i] = this;
		}
	);
	if(links.length > 0)
	{
	    Shadowbox.init({
		    loadingImage: 		'../global/img/shadowbox/loading.gif',
		    overlayBgImage:     '',
		    overlayOpacity:     0.90
	    });
		Shadowbox.setup(links, {showTitle: true, titleHeight: "20px"});
	}
	
	/*popup floorplans behaviour*/
	/*floorplans set up*/
	if(document.getElementById("popup-floorplans"))
	{
	    loc = window.location + "";
        if(loc.indexOf("&bedrooms")!==-1)
        {
            si = loc.indexOf("&bedrooms") + 10;
            li = loc.indexOf("&", si);
            if(li < 0){li = loc.length;}
            num = loc.substring(si, li);
            num = parseInt(num, 10);
            num = num-1;
            
            /*hide the default selected items*/
            $("div.popup-floorplans div.floorplan-menus div.header-box div.body ul li").removeClass("selected");
            $("div.floorplans div.fp").addClass("hidden").removeClass("selected");
            
            /*show the corrent elements*/
            $("div.popup-floorplans div.floorplan-menus div.header-box:eq("+num+") div.body ul li:eq(0)").addClass("selected");
            id = $("div.popup-floorplans div.floorplan-menus div.header-box:eq("+num+") div.body ul li:eq(0)").attr("id");
            $("div.floorplans div."+id).addClass("selected").removeClass("hidden");
            
            if(num > 0)
            {
                //then change the navigation list showing
                $("div.popup-floorplans div.floorplan-menus div.open").removeClass("open").addClass("closed");
                $("div.popup-floorplans div.floorplan-menus div.header-box:eq("+num+")").removeClass("closed").addClass("open");
            }
            
        }
        sifrFPShowing();
	}
	$("div.popup-floorplans div.header-box div.body ul li a").click(
	    function()
	    {
	       //get the id of the parent li
	       liid = $(this).parents("li").attr("id");
	       $("div.floorplans div.fp").addClass("hidden").removeClass("selected");
	       $("."+liid).removeClass("hidden");
	      	       
	       $("div.popup div.header-box div.body ul li").removeClass("selected");
	       $("#"+liid).addClass("selected");
	       $("."+liid).addClass("selected");
	       
	       //behaviour for floorplans
	        el = $("div.floorplans div.selected div.state-in div.drag-overlay");
            elmove = $("div.floorplans div.selected div.state-in img.in");
            assignDrag(el, elmove);
            
            //sifr elements
            sifrFPShowing();
            
            return false;
	    }
	);
	
	$(".popup div.controls a.zoomin").click(
	    function()
	    {
	        $("div.floorplans div.selected div.plan").removeClass("state-out").addClass("state-in");
            el = $("div.floorplans div.selected div.state-in div.drag-overlay");
            elmove = $("div.floorplans div.selected div.state-in img.in");
            assignDrag(el, elmove);
	        return false;
	    }
	);
	
	$(".popup div.controls a.zoomout").click(
	    function()
	    {
	        $("div.floorplans div.selected div.plan").removeClass("state-in").addClass("state-out");
	        return false;
	    }
	);
	
	$(".popup div.controls a.print").click(
	    function()
	    {
	        window.print();
	    }
	);
	
	$(".popup div.controls a.dwnldpdf").click(
	    function()
	    {
	    
	        $(this).attr("href", $("div.floorplans div.selected div.plan").attr("id"));
	        parent.window.location = $("div.floorplans div.selected div.plan").attr("id");
	        return false; 
	    }
	);
	
	/*popup menu expanders for floorplans*/
	$("div.popup div.expander-menus div.header-box div.head a").click(
	    function()
	    {
	        if(($(this).parents(".header-box").attr("class").match("closed")))
	        {
	            $(this).parents(".header-box").children(".body").slideDown("slow");
	            $("div.popup div.expander-menus div.open div.body").slideUp("slow");
	            $("div.popup div.expander-menus div.open").removeClass("open").addClass("closed");
	            $(this).parents(".header-box").removeClass("closed").addClass("open");
				
				//show the first item from that list
				//get the id of the parent li
		       liid = $("div.popup-floorplans div.expander-menus div.open div.body ul li:first").attr("id");
		       $("div.floorplans div.fp").addClass("hidden").removeClass("selected");
		       $("."+liid).removeClass("hidden");
		      	       
		       $("div.popup div.header-box div.body ul li").removeClass("selected");
		       $("#"+liid).addClass("selected");
		       $("."+liid).addClass("selected");
		       
		       //behaviour for floorplans
		        el = $("div.floorplans div.selected div.state-in div.drag-overlay");
	            elmove = $("div.floorplans div.selected div.state-in img.in");
	            assignDrag(el, elmove);
	            
	            //sifr elements
	            sifrFPShowing();
				
	        }
	        
	        return false;
	        
	    }
	);
	
	if(document.getElementById("popup"))
	{
        el = $("div.floorplans div.selected div.state-in div.drag-overlay");
        elmove = $("div.floorplans div.selected div.state-in img.in");
        assignDrag(el, elmove);
    }
    
    /*popup views behaviour*/
    $("div.popup-views div.header-box div.body ul li a").click(
        function()
        {
           //get the id of the parent li
	       liid = $(this).parents("li").attr("id");	      	       
	       $("div.popup div.header-box div.body ul li").removeClass("selected");
	       $("#"+liid).addClass("selected");
	       
	       //the swf to embed
	       swf = $(this).attr("href").replace("#", "");
	       path = "../global/swf/floorviews/"+swf;
	       
	       var so = new SWFObject(path, "flashObj", "677", "581", "9", "#FFFFFF");
		    so.addParam("wmode", "transparent");
		    //so.addVariable("variable1", "value1");
		    so.write("viewsFlash");
        }
    );
    /*popup-views load*/
    if(document.getElementById("viewsFlash"))
    {
        var so = new SWFObject("../global/swf/floorviews/level18.swf", "flashObj", "677", "581", "9", "#FFFFFF");
	    so.addParam("wmode", "transparent");
	    so.write("viewsFlash");
    }
    
    /*popup gallery behaviour*/
    if(document.getElementById("popup-gallery"))
    {
        //see if theres an attribute for which gallery needs to be showing on load
        loc = window.location + "";
        if(loc.indexOf("&gallery")!==-1)
        {
            si = loc.indexOf("&gallery") + 9;
            li = loc.indexOf("&", si);
            if(li < 0){li = loc.length;}
            id = loc.substring(si, li);
            
            $("div.popup-gallery div.tab-container").addClass("hidden");
            $("div.popup-gallery div#"+id).removeClass("hidden");
            $("div.popup-gallery div.tabs li").removeClass("selected");
            $("div.popup-gallery div.tabs li."+id).addClass("selected");
        }
        //set up controls for each gallery
        $("div.popup-gallery div.tab-container").each(
            function(i)
            {
                count = 0;
                $(this).children(".item").each(
                    function()
                    {
                        count++;
                    }
                );
                html = "Item <span class='curr'>1</span> of <span class='total'>" + count + "</span>";
                $(this).children(".controls").children(".count").children("p").html(html);
            }
        );
	    //tabs swap
	    $("div.popup-gallery div.tabs ul li a").click(
	        function()
	        {
	            id = $(this).attr("href").replace("#", "");
	            $("div.tab-container").addClass("hidden");
	            $("div.tabs ul li").removeClass("selected");
	            $(this).parents("li").addClass("selected");
	            $("#"+id).removeClass("hidden");
	            return false;
	        }
	    );
	    //next behaviour
	    $("div.popup-gallery div.tab-container div.controls div.next a").click(
	        function()
	        {
	            if(!($(this).parents(".next").attr("class").match("next-disabled")))
	            {
	                //get the current gallery id
	                id = $(this).parents(".tab-container").attr("id");
	                //get the number of the current showing element
	                num = parseInt($("#"+id+" div.controls div.count span.curr").html())-1;
	                next = num+1;
    	            
	                //animate the images	   
                    //updates the count after fade complete         
                    $("#"+id+" div.item:eq("+num+")").fadeOut(2000, function(){$("#"+id+" div.controls div.count span.curr").html(next+1);});
                    $("#"+id+" div.item:eq("+next+")").fadeIn(2000);
    	            
    	            
	                total = parseInt($("#"+id+" div.controls div.count span.total").html());
	                if((next+1) == total)
	                {
	                    $("#"+id+" div.controls div.next").addClass("next-disabled");
	                }
	                if(next == 1)
	                {
	                    $("#"+id+" div.controls div.prev").removeClass("prev-disabled");
	                }
	            }
	            return false;
	        }
	    );
    	
	    //prev behaviour
	    $("div.popup-gallery div.tab-container div.controls div.prev a").click(
	        function()
	        {
	            if(!($(this).parents(".prev").attr("class").match("prev-disabled")))
	            {
	                //get the current gallery id
                    id = $(this).parents(".tab-container").attr("id");
                    //get the number of the current showing element
                    num = parseInt($("#"+id+" div.controls div.count span.curr").html())-1;
                    next = num-1;
                    
                    //animate the images	            
                    //updates the count after fade complete
                    $("#"+id+" div.item:eq("+num+")").fadeOut(2000, function(){$("#"+id+" div.controls div.count span.curr").html(next+1);});
                    $("#"+id+" div.item:eq("+next+")").fadeIn(2000);
                    
                    
                    total = parseInt($("#"+id+" div.controls div.count span.total").html());
                    if(next == 0)
                    {
                        $("#"+id+" div.controls div.prev").addClass("prev-disabled");
                    }
                    if(next < total)
                    {
                        $("#"+id+" div.controls div.next").removeClass("next-disabled");
                    }
                }
                return false;
	        }
	    );
	}
	
});

function sifrFPShowing()
{
    sIFR.replace(helveticaneueExtendedLight, {
	    selector: 'div.popup div.floorplans div.selected div.info h3',
	    css: [
            '.sIFR-root {color: #00457C;}'
          ]
    });
    sIFR.replace(helveticaneueExtendedLight, {
	    selector: 'div.popup div.floorplans div.selected div.info ul li',
	    css: [
            '.sIFR-root {color: #00457C; leading: -3px;}'
          ]
    });
    sIFR.replace(helveticaneueExtendedLight, {
	    selector: 'div.popup div.floorplans div.selected div.info table td span',
	    css: [
            '.sIFR-root {color: #00457C;}'
          ]
    });
    sIFR.replace(helveticaneueExtendedLight, {
	    selector: 'div.popup div.floorplans div.selected div.info p',
	    css: [
            '.sIFR-root {color: #00457C;}'
          ]
    });
}

function assignDrag(el, elmove)
{
    clicked = false;
    start_x = 0;
	start_y = 0;
    el.mousedown(
	    function(e)
	    {
	        $("div.popup div.views div.help").remove();
	        clicked = true;
	        start_x = Math.round(e.pageX - $(this).eq(0).offset().left);
	        start_y = Math.round(e.pageY - $(this).eq(0).offset().top);
	        el.css("cursor", "move");
	    }
	);
	el.mouseup(
	    function(e)
	    {
	        clicked = false;
	        el.css("cursor", "pointer");
	    }
	);
	el.mousemove(
	    function(e)
	    {
	        if(clicked == true)
	        {
	            //get the current position of the img
	            currx = parseInt(elmove.css("left").replace("px", ""));
	            curry = parseInt(elmove.css("top").replace("px", ""));
	            //get the new mouse position
	            mousex = Math.round(e.pageX - $(this).eq(0).offset().left);
	            mousey = Math.round(e.pageY - $(this).eq(0).offset().top);
	            //find the diff
	            diffx = mousex - start_x;
	            diffy = mousey - start_y;
	            //get the new position
	            newx = currx + diffx;
	            newy = curry + diffy;
	            //check max positioning of the image
	            if(newx > 0){newx = 0;}
	            if(newy > 0){newy = 0;}
	            //get the min positions allowable
	                elwidth = el.width();
	                elheight = el.height();
	                elmoveWidth = elmove.width();
	                elmoveHeight = elmove.height();
	                minx = elwidth - elmoveWidth;
	                miny = elheight - elmoveHeight;
	            //check min positioning of the image
	            if(newx < minx){newx = minx;}
	            if(newy < miny){newy = miny;}
	            //assign the new position
	            elmove.css("left", newx+"px");
	            elmove.css("top", newy+"px");
	            //update the previous mouse position
	            start_x = mousex;
	            start_y = mousey;
	        }
	    }
	);
}

function changeViewPlansLink(id, num)
{
    //get the id of the current li that is showing
    liid = $("#"+id+" .jcarousel-list li:nth-child("+num+")").attr("id");
    $("div.viewplans-links li").addClass("hidden");
    //show the right link the view plans button
    $("div.viewplans-links li."+liid).removeClass("hidden");
}

function enableJSClass()
{
	document.body.className = "jsEnabled";
}

function closeShadowbox()
{
    parent.Shadowbox.close();
}