// page animations

//select urlname
var j = (document.URL.substring(document.URL.lastIndexOf("\/")+1));

//enquiry
$('#enquire').hide().delay(2000).slideDown(500);


//add selected to submenu
$('#submenu a[href="' + j + '"]').addClass("selected");

//navbar highlight
$('#navbar a[href="' + j + '"]').parent('li').parent('ul').parent('li').children('a').addClass("selected");
$('#navbar a[href="' + j + '"]').parent('li').children('a').addClass("selected");

//submenu open
$('#submenu a[href="' + j + '"]').parent('li').parent('ul').removeClass();
$('#submenu a[href="' + j + '"]').parent('li').children('ul').removeClass();

var randomnumber=Math.floor(Math.random()*5)

// footer appear
$(window).load(function(){
$("#floatingfooter").delay(1000).slideDown("slow");
})

// submenu down
$("ul.hidden").parent("li").prepend("<a class='expand'></a> ");
$("#submenu ul li ul:visible").parent("li").prepend("<a class='contract'></a> ");

$("#submenu a.contract").live('click', (function()
{
	$(this).parent("li").children("ul").slideUp(500);
	$(this).parent("li").prepend("<a class='expand'></a>");
	$(this).parent("li").children("a.contract").remove();
	}	
));

$("#submenu a.expand").live('click', (function()
{
	$(this).parent("li").children("ul").slideDown(500);
	$(this).parent("li").prepend("<a class='contract'></a>");
	$(this).parent("li").children("a.expand").remove();
	}	
));




// sub slide toggle
//$("#submenu li").hover(function()
//{
//$(this).children("ul.hidden").slideToggle(500);}
//);


// nav slide
//$("#navbar li").hover(function()
//{$(this).children("ul").slideToggle(200);}
//);

$("#navbar li").mouseenter(function()
{$(this).children("ul:hidden").slideDown(400);}
);
$("#navbar li").mouseleave(function()
{$(this).children("ul:visible").delay(50).slideUp(200);}
);


// footer nav slide
//$("#floatingfooter li").hover(function()
//{$(this).children("ul").slideToggle(200);}
//);

$("#floatingfooter li").mouseenter(function()
{$(this).children("ul:hidden").slideDown(400);}
);
$("#floatingfooter li").mouseleave(function()
{$(this).children("ul:visible").delay(50).slideUp(200);}
);



$(window).load(function(){
$('body').append('<div class="hover"><div align="center">Bookmark and <br />Share this Page<br /><img src="images/share-this-page.png" width="49" height="65" /></div></div>');
						
$(".hover").hide().delay(4000+(1000*randomnumber)).animate({"bottom": "-=150px", "opacity": "toggle"}, 500, "swing" ).delay(4000).fadeOut(5000);
})

// IMAGE ENLARGE

var defx 
var defy
$('body').prepend('<div id="overlay">Click to Close</div><div id="enlarged"></div>');

//
//$(window).load(function() {	
// while(i < $('img.enlarge').length){
//$('img.enlarge:eq('+i+')').animate({'opacity': .8 },400).delay(200).animate({'opacity': 1 },400);
//i++;
// }
//});

$(window).ready(function() {	
i=0;
while(i < $('img.enlarge').length){
	var img = new Image();
	nn = $('img.enlarge:eq('+i+')').attr('src').replace('-prowater','');
	$(img).attr("src", nn);
i++;
 }
});




$("img.enlarge").live('mouseenter', (function()
{
	var img = new Image();
	$(this).animate({'opacity': .8 },200)

}
));

$("img.enlarge").live('mouseleave', (function()
{
	$(this).animate({'opacity': 1 },200)

}
));

$("img.enlarge").live('click', (function()
{

$("#floatingfooter").slideUp(500);
var j = $(this).attr('src').replace('-prowater','');
var t = $(this).attr('title');

var smix = $(this).attr('width');
var smiy = $(this).attr('height');
var smx = ($(window).width() - ( $(this).attr('width')+20)) / 2;  smx = (smx / $(window).width()) * 100;
var smy = ($(window).height() - ($(this).attr('height')+50)) / 2;  smy = (smy / $(window).height()) * 100;

$('#enlarged').css({'position': 'absolute', 'top': -9999 }).show();
$('#enlarged').append('<img src="'+j+'"/><div>'+t+'</div>');
var wid = $('#enlarged').children('img').attr('width');   defx = wid;
var hei = $('#enlarged').children('img').attr('height');   defy = hei;

if(wid > $(window).width() - 40)
{
var ow = wid;
wid = $(window).width() - 40;
var ratio = wid/ow;
hei = hei * ratio;
}

if(hei > $(window).height() - 60)
{ 
var oh = hei;
hei = $(window).height() - 60;
var ratio = hei/oh;
wid = wid * ratio;
}

$('#enlarged').children('img').attr({'width': wid,'height': hei});


var xpos = ($(window).width() - (wid+20)) / 2;  xpos = (xpos / $(window).width()) * 100;
var ypos = ($(window).height() - (hei+50)) / 2;  ypos = (ypos / $(window).height()) * 100;
$('#enlarged').children('img').hide();
$('#enlarged').children('img').css({'width': smix, 'height': smiy, 'opacity': 0}).show();
$('#enlarged').children('div').hide();
$('#enlarged').hide().css({'position': 'fixed', 'left': smx+'%', 'top': smy+'%', 'width': $(this).attr('width'), 'height': $(this).attr('height'), 'opacity': 0}).show();
$('#enlarged').animate({
					   'left': xpos+'%',
					   'top': ypos+'%',
					   'width': wid, 
					   'height': hei+30,
					   'opacity': 1 }, 500);

$('#enlarged').children('img').animate({
					   'width': wid, 
					   'height': hei,
					   'opacity': 1 }, 500);
$('#enlarged').children('div').delay(600).fadeIn();

  	$('#overlay').css({'filter': 'alpha(opacity=60)', 'opacity': '0.6'} );
	$('#overlay').fadeIn(300);
	$('#enlarged').delay(100).fadeIn(400);
	}
));

$("#enlarged").live('click', (function()
{
	$("#floatingfooter").slideDown(500);
	$('#overlay').delay(100).fadeOut(300);
	$('#enlarged').fadeOut(400, function(){ $('#enlarged').children('img').remove(); $('#enlarged').children('div').remove() } );
	

	}	
));
$("#overlay").live('click', (function()
{
	$("#floatingfooter").slideDown(500);
	$('#overlay').delay(100).fadeOut(300);
	$('#enlarged').fadeOut(400, function(){ $('#enlarged').children('img').remove(); $('#enlarged').children('div').remove() } );
	

	}	
));



$(window).resize(function() {
var wid = $('#enlarged').children('img').attr('width');
var hei = $('#enlarged').children('img').attr('height');						  

if(hei > $(window).height() - 60)
{ 
var oh = hei;
hei = $(window).height() - 60;
var ratio = hei/oh;
wid = wid * ratio;
}
if($(window).height() - 60 > hei)
{
var oh = hei;	
hei = $(window).height() - 60;
if(hei > defy){hei=defy}
var ratio = hei/oh;
wid = wid * ratio;
}


$('#enlarged').children('img').animate({'width': wid,'height': hei},100);
var xpos = ($(window).width() - (wid+20)) / 2;  xpos = (xpos / $(window).width()) * 100;
var ypos = ($(window).height() - (hei+50)) / 2;  ypos = (ypos / $(window).height()) * 100;
$('#enlarged').animate({
					   'left': xpos+'%',
					   'top': ypos+'%',
					   'width': wid, 
					   'height': hei+30,
					   'opacity': 1 }, 100);
});

