/* JavaScript Document
*  Creare by weiwei for Novena main javascript
*  2011-04-02
*/

var winHeight = 0;
var winWidth = 0;
function findDimensions(){
	//got window width
	if (window.innerWidth)
		winWidth = window.innerWidth;
	else if ((document.body) && (document.body.clientWidth))
		winWidth = document.body.clientWidth;
	//got window height
	if (window.innerHeight)
		winHeight = window.innerHeight;
	else if ((document.body) && (document.body.clientHeight))
		winHeight = document.body.clientHeight;

	if (document.documentElement  && document.documentElement.clientHeight && document.documentElement.clientWidth)
	{
		winHeight = document.documentElement.clientHeight;
		winWidth = document.documentElement.clientWidth;
	}	
	
	//Homepage background image banner resize
	var ratio = winWidth/winHeight;
	if(ratio > 1.6){
		$('#supersized a').each(function(){
			$(this).children().width(winWidth);
			$(this).children().height(winWidth/1.6);
		});
	}else{
		$('#supersized a').each(function(){
			$(this).children().width(1.6*winHeight);
			$(this).children().height(winHeight);
		});
	}
	
	$('#controls-wrapper').css("display","block");
	//Homepage background image banner button position | min-width='1024px'
	if(winWidth <= 960){
		$('#controls-wrapper').css({
			width:'960px',
			height:0,
			left:0,
			top:(winHeight/2 - 35) + 'px'
		});
	}
	else if(winWidth <= 1100 && winWidth > 960){
		$('#controls-wrapper').css({
			width:'960px',
			height:0,
			left:(winWidth - 960)/2 + 'px',
			top:(winHeight/2 - 35) + 'px'
		});
		//$('#footer_home').css("left",(winWidth - 960)/2 + "px");
		$('#footer').css("left",(winWidth - 960)/2 + "px");
	}
	else if(winWidth > 1100){
		$('#controls-wrapper').css({
			width:(winWidth - 140) + 'px',
			height:0,
			left:'70px',
			top:(winHeight/2 - 35) + 'px'
		});
		//$('#footer_home').css("left",(winWidth - 960)/2 + "px");
		$('#footer').css("left",(winWidth - 960)/2 + "px");
	}
	
	//footer position
	//$('#footer_home').css("top",winHeight - 36);
	if(winHeight > 699){
		$('#footer').css("top",winHeight - 36);
	}else{
		$('#footer').css("top","699px");
	}
	
} 


$(function(){
	findDimensions();
	window.onresize=findDimensions;
	
	//Homepage background image banner button hover function
	$('#controls-wrapper img').hover(function(){
		$(this).css({
			filter:'alpha(opacity=100)', 
			opacity: '1'	
		});
	},
	function(){
		$(this).css({
			filter:'alpha(opacity=50)', 
			opacity: '0.5'	
		});
	});
		
	//Search function
	$('.search_text').focus(function() {
		$(this).val("");
	});
	$('.search_text').blur(function(){
		if($(this).val() == ''){
			$(this).val('SEARCH');
		}
	});
	
	//information tabbing function
	$('.information_tab li a').click(function() {
		$('.information_tab li').each(function() {
			$(this).children().removeClass('current_tab');
		});
		$(this).addClass('current_tab');
		for(var i=1; i<6; i++){
			$('.tab_cont_' + i).addClass('none');
		}
		var currTab = $(this).parent().index()+1;
		$('.tab_cont_' + currTab).removeClass('none');;
	});
	
	//footer button click function | will popup a div
	/*$('.lucky_draw').click(function(){
		var luckyHeight = $('.lucky_draw_popup').height();
		$('.lucky_draw_popup').animate({top: luckyHeight + 'px'}, "slow");
	});*/
	
	//our shops page | click shops will show its details
	$('.shops_table tr td').click(function(){
		var shopsNo = $(this).parent().index() + 1;
		var shopsLength = $('.cont_right_1 .shops_cate').length;
		for(var i=1; i<=shopsLength; i++){
			$('.shops_' + i).addClass('none');
		}
		$('.shops_' + shopsNo).removeClass('none');
	});
	
	$('.shops_table_1 tr td').click(function(){
		var shopsNo = $(this).parent().index() + 1;
		var shopsLength_1 = $('.cont_right_1 .shops_level1').length;
		var shopsLength_2 = $('.cont_right_1 .shops_level2').length;
		for(var i=1; i<=shopsLength_1; i++){
			$('.shops_level1_' + i).addClass('none');
		}
		for(var i=1; i<=shopsLength_2; i++){
			$('.shops_level2_' + i).addClass('none');
		}
		$('.shops_level1_' + shopsNo).removeClass('none');
	});
	
	$('.shops_table_2 tr td').click(function(){
		var shopsNo = $(this).parent().index() + 1;
		var shopsLength_1 = $('.cont_right_1 .shops_level1').length;
		var shopsLength_2 = $('.cont_right_1 .shops_level2').length;
		for(var i=1; i<=shopsLength_1; i++){
			$('.shops_level1_' + i).addClass('none');
		}
		for(var i=1; i<=shopsLength_2; i++){
			$('.shops_level2_' + i).addClass('none');
		}
		$('.shops_level2_' + shopsNo).removeClass('none');
	});
	
	$('.shops_table_1 tr td:last-child').addClass('last_td');
	$('.shops_table_2 tr td:last-child').addClass('last_td');
	
	//register steps | NIRC and OR FIN/Passport No. input default value
	$('.newsletter_table input.nric_input,.newsletter_table input.passport_input').focus(function(){
		$(this).val("");
	});
	$('.newsletter_table input.nric_input').blur(function(){
		if($(this).val() == ''){
			$(this).val('NRIC');
		}
	});
	$('.newsletter_table input.passport_input').blur(function(){
		if($(this).val() == ''){
			$(this).val('OR FIN/Passport No.');
		}
	});
	
	//register step2/2 collopse menu function
	$('.step2_menu > li > a').click(function(){
		if($(this).hasClass('current_menu')){
			$(this).removeClass('current_menu');
			$(this).next().slideUp();
		}else{
			$('.step2_menu > li').each(function(){
				$(this).children('a').removeClass('current_menu');
				$(this).children('ul').slideUp();
			});
			$(this).addClass('current_menu');
			$(this).next().slideDown();
		}
	});
	
	//Click Lucky Draw button will popup a box in the footer
	/*var popupHeight1 = $('.lucky_draw_popup').height();
	$('.lucky_draw_popup').height(0);
	$(".lucky_draw").click(function(){
		$(".tweets").removeClass('footer_tab_curr');
		var popupTop = - popupHeight1;
		$('.tweets_popup').animate({
				top: 0,
				height:0
			}, 200);
		if($('.lucky_draw_popup').css("top") == '0px'){
			$(this).addClass('footer_tab_curr');
			$('.lucky_draw_popup').animate({
				top: popupTop,
				height:popupHeight1
			}, 400);
		}else{
			$(this).removeClass('footer_tab_curr');
			$('.lucky_draw_popup').animate({
				top:0,
				height:0
			}, 400);
		}
	});*/
	
	//Click Lucky Draw Winners button will popup a box in the footer
	var popupHeightWinner = $('.winners_popup').height();
	$('.winners_popup').height(0);
	$(".lucky_draw_winners").click(function(){
		$(".whatshot").removeClass('footer_tab_curr');
		$(".lucky_draw").removeClass('footer_tab_curr');
		$(".tweets").removeClass('footer_tab_curr');
		var popupTop = - popupHeightWinner;
		$('.lucky_draw_popup').animate({
				top: 0,
				height:0
			}, 200);
		$('.whatshot').animate({
				top: 0,
				height:0
			}, 200);
		$('.tweets_popup').animate({
				top: 0,
				height:0
			}, 200);
		if($('.winners_popup').css("top") == '0px'){
			$(this).addClass('footer_tab_curr');
			$('.winners_popup').animate({
				top: popupTop,
				height:popupHeightWinner
			}, 400);
		}else{
			$(this).removeClass('footer_tab_curr');
			$('.winners_popup').animate({
				top: 0,
				height:0
			}, 400);
		}
	});
	
	//Click TWEETS button will popup a box in the footer
	var popupHeight2 = $('.tweets_popup').height();
	$('.tweets_popup').height(0);
	$(".tweets").click(function(){
		$(".whatshot").removeClass('footer_tab_curr');
		$(".lucky_draw").removeClass('footer_tab_curr');
		$(".lucky_draw_winners").removeClass('footer_tab_curr');
		var popupTop = - popupHeight2;
		$('.whatshot_popup').animate({
				top: 0,
				height:0
			}, 200);
		$('.lucky_draw_popup').animate({
				top: 0,
				height:0
			}, 200);
		$('.winners_popup').animate({
				top: 0,
				height:0
			}, 200);
		if($('.tweets_popup').css("top") == '0px'){
			$(this).addClass('footer_tab_curr');
			$('.tweets_popup').animate({
				top: popupTop,
				height:popupHeight2
			}, 400);
		}else{
			$(this).removeClass('footer_tab_curr');
			$('.tweets_popup').animate({
				top: 0,
				height:0
			}, 400);
		}
	});
	
	//Click WHAT'S HOT button will popup a box in the footer
	var popupHeight3 = $('.whatshot_popup').height();
	var max_num = 0;
	$(".gallery_details").each(function(index, domEle) {
		// domeEle = this
		var temp = $(domEle).height();
		if(temp > max_num)
		{
			max_num = temp;
		}
	});
	
	$('.whatshot_popup').height(0);
	$(".whatshot").click(function(){
		$(".lucky_draw").removeClass('footer_tab_curr');
		$(".lucky_draw_winners").removeClass('footer_tab_curr');
		$(".tweets").removeClass('footer_tab_curr');
		var popupTop = - (max_num + 278);
		$('.lucky_draw_popup').animate({
				top: 0,
				height:0
			}, 200);
		$('.winners_popup').animate({
				top: 0,
				height:0
			}, 200);
		$('.tweets_popup').animate({
				top: 0,
				height:0
			}, 200);
		if($('.whatshot_popup').css("top") == '0px'){
			$(this).addClass('footer_tab_curr');
			$('.whatshot_popup').animate({
				top: popupTop,
				height:max_num+278
			}, 400);
		}else{
			$(this).removeClass('footer_tab_curr');
			$('.whatshot_popup').animate({
				top: 0,
				height:0
			}, 400);
		}
	});
	
	$('.close_x').click(function(){
		$('.whatshot_popup').animate({
				top: 0,
				height:0
			}, 400);
		$('.lucky_draw_popup').animate({
				top: 0,
				height:0
			}, 400);
		$('.tweets_popup').animate({
				top: 0,
				height:0
			}, 400);
		$('.winners_popup').animate({
				top: 0,
				height:0
			}, 400);
		$(".whatshot").removeClass('footer_tab_curr');
		$(".lucky_draw").removeClass('footer_tab_curr');
		$(".lucky_draw_winners").removeClass('footer_tab_curr');
		$(".tweets").removeClass('footer_tab_curr');
	});
	
	//greybox
	$("a.greybox_thumbnail").click(function(){
        var t = this.title || this.innerHTML || this.href;
        GB_show('',this.href,155,224);
        return false;
    });
	
	//what's hot photo gallery
	/*$('#slide_wrapper').hover(function(){
		$('.balck_bg').animate({
			marginTop: '146px',
			height:'38px'
		},500);
		$('.dot li').animate({
			height:'38px'
		},500);
		$('.dot li.currdot').animate({
			height:'38px'
		},500);
	},
	function(){
		$('.balck_bg').animate({
			marginTop: '184px',
			height:'0px'
		},500);
		$('.dot li').animate({
			height:'0px'
		},500);
		$('.dot li.currdot').animate({	
			height:'0px'
		},500);
		
	});
	
	$('.dot li').click(function(){
		var dotNum = $(this).index();
		$('#slideshow').animate({left:(-355*dotNum) + 'px'},500);
		$('.dot li.currdot').removeClass('currdot');
		$('.dot li').eq(dotNum).addClass('currdot');
		$('.whatshot_popup .gallery_details').each(function(){
			$(this).addClass('none');
		});
		$('.whatshot_popup .gallery_details').eq(dotNum).removeClass('none');
	});*/
	
	/*var lengthImg = $('#slideshow li').length;
	var galleryValue = 1;
	function galleryLoop(){
		$('#slideshow').animate({left:(-355*galleryValue) + 'px'},500);
		$('.dot li.currdot').removeClass('currdot');
		$('.dot li').eq(galleryValue).addClass('currdot');
		$('.whatshot_popup .gallery_details').each(function(){
			$(this).addClass('none');
		});
		$('.whatshot_popup .gallery_details').eq(galleryValue).removeClass('none');
		if(galleryValue < lengthImg - 1){
			galleryValue ++;
		}else{
			galleryValue = 0;
		}
	}
	
	setInterval(galleryLoop,3000);
	
	$('.prev').click(function(){
		var dotNum = $('.dot li.currdot').index();
		if(dotNum == 0){
			$('#slideshow').animate({left:(-355*(lengthImg - 1)) + 'px'},500);
			$('.dot li.currdot').removeClass('currdot');
			$('.dot li').eq(lengthImg - 1).addClass('currdot');
			$('.whatshot_popup .gallery_details').each(function(){
				$(this).addClass('none');
			});
			$('.whatshot_popup .gallery_details').eq(lengthImg - 1).removeClass('none');
		}else{
			$('#slideshow').animate({left:(-355*(dotNum - 1)) + 'px'},500);
			$('.dot li.currdot').removeClass('currdot');
			$('.dot li').eq(dotNum - 1).addClass('currdot');
			$('.whatshot_popup .gallery_details').each(function(){
				$(this).addClass('none');
			});
			$('.whatshot_popup .gallery_details').eq(dotNum - 1).removeClass('none');
		}
	});
	
	$('.next').click(function(){
		var dotNum = $('.dot li.currdot').index();
		if(dotNum == lengthImg - 1){
			$('#slideshow').animate({left: '0px'},500);
			$('.dot li.currdot').removeClass('currdot');
			$('.dot li').eq(0).addClass('currdot');
			$('.whatshot_popup .gallery_details').each(function(){
				$(this).addClass('none');
			});
			$('.whatshot_popup .gallery_details').eq(0).removeClass('none');
		}else{
			$('#slideshow').animate({left:(-355*(dotNum + 1)) + 'px'},500);
			$('.dot li.currdot').removeClass('currdot');
			$('.dot li').eq(dotNum + 1).addClass('currdot');
			$('.whatshot_popup .gallery_details').each(function(){
				$(this).addClass('none');
			});
			$('.whatshot_popup .gallery_details').eq(dotNum + 1).removeClass('none');
		}
	});*/
	
	var lengthImg = $('#slideshow li').length;
	$('#slideshow').css("width",lengthImg*355);
	var gallery_interval = setInterval(galleryLoop,3000);
	function galleryLoop(){
		$('#slideshow').animate({left:'-355px'},500,function(){
			$('.slide_cont .gallery_details').each(function(){
				$(this).addClass('none');
			});
			$('.slide_cont .gallery_details').eq(1).removeClass('none');
			$('#slideshow li').eq(0).appendTo('#slideshow');
			$('.slide_cont .gallery_details').eq(0).appendTo('.slide_cont');
			$('#slideshow').css("left","0");
		});
	}
	
	$('.prev').click(function(){
		clearInterval(gallery_interval);
		$('#slideshow li').eq(lengthImg - 1).prependTo('#slideshow');
		$('.slide_cont .gallery_details').eq(lengthImg - 1).prependTo('.slide_cont');
		$('#slideshow').css("left","-355px");
		$('#slideshow').animate({left:'0px'},500,function(){
			$('.whatshot_popup .gallery_details').each(function(){
				$(this).addClass('none');
			});
			$('.whatshot_popup .gallery_details').eq(0).removeClass('none');
		});
		gallery_interval = setInterval(galleryLoop,3000);
	});
	
	$('.next').click(function(){
		clearInterval(gallery_interval);
		galleryLoop();
		gallery_interval = setInterval(galleryLoop,3000);
	});
	
	if($('#slideshow li').length == 1){
		$('.prev').addClass('none');
		$('.next').addClass('none');
	}else{
		$('.prev').removeClass('none');
		$('.next').removeClass('none');
	}
	
	/*var dotNo = $('.dot li').length;
	var dotWidth = 42*dotNo;
	var dotLeft = (355-dotWidth)/2;
	$('.dot').css({
		width:dotWidth,
		marginLeft:dotLeft
	});*/
	
});
