$(document).ready(function() {
	//clear flags
	$('#flags input').val(0);

	//Change css for smaller window height
	var $wh=$(window).height();
	if($wh<750){
		$('#splash').css('top','518px');
		$('#about-loader').css('height','240px');
		$('#contact').css('top','116px');
	}

	//populate function on sequence image loading
	function load_contact(){
		var el=$('#flag_contact');
		var x=el.val();
		if(x==0){
			el.val(1);
			var contact_images=new Array();
			contact_images[0]='images/bottle1.png';
			contact_images[1]='images/stars.png';
			contact_images[2]='images/clouds.png';
			contact_images[3]='images/monkey-rocket.png';
			$({}).imageLoader({
				images: contact_images,
				async: false,
				complete: function( e, ui ) {
					var n=ui.i;
					if(n==0)	$('#contact #bottle1').css('background','url('+contact_images[n]+') 0 0 no-repeat');
					if(n==1)	$('.stars').css('background','url('+contact_images[n]+') 0 0 repeat-x');
					if(n==2)	$('.clouds').css('background','url('+contact_images[n]+') top center no-repeat');
					if(n==3)	$('.rocket').css('background','url('+contact_images[n]+') top center no-repeat');
				},
				allcomplete: function(e, ui) {

				}
			});
		}
	}

	function load_about(){
		var el=$('#flag_about');
		var x=el.val();
		if(x==0){
			el.val(1);
			var about_images=new Array();
			about_images[0]='images/tagabukid-top.png';
			about_images[1]='images/plants/cebu.png';
			about_images[2]='images/plants/davao.png';
			about_images[3]='images/plants/laguna.png';
			about_images[4]='images/plants/luzon.png';
			about_images[5]='images/plants/mindanao.png';
			about_images[6]='images/plants/pswr_cebu.jpg';
			about_images[7]='images/plants/pswr_davao.jpg';
			about_images[8]='images/plants/pswr_laguna.jpg';
			about_images[9]='images/plants/pswr_luzon.jpg';
			about_images[10]='images/plants/pswr_mindanao.jpg';
			$({}).imageLoader({
				images: about_images,
				async: false,
				complete: function( e, ui ) {
					var n=ui.i;
					if(n==0)	$('.land1').css('background','url('+about_images[n]+') center bottom no-repeat');
					if(n==1)	$('#kwick1').prepend('<img class="" src="'+about_images[n]+'"  />')
					if(n==2)	$('#kwick2').prepend('<img class="" src="'+about_images[n]+'"  />')
					if(n==3)	$('#kwick3').prepend('<img class="" src="'+about_images[n]+'"  />')
					if(n==4)	$('#kwick4').prepend('<img class="" src="'+about_images[n]+'"  />')
					if(n==5)	$('#kwick5').prepend('<img class="" src="'+about_images[n]+'"  />')
					if(n==6)	$('#kwick1').prepend('<img class="plant" src="'+about_images[n]+'"  />')
					if(n==7)	$('#kwick2').prepend('<img class="plant" src="'+about_images[n]+'"  />')
					if(n==8)	$('#kwick3').prepend('<img class="plant" src="'+about_images[n]+'"  />')
					if(n==9)	$('#kwick4').prepend('<img class="plant" src="'+about_images[n]+'"  />')
					if(n==10)	$('#kwick5').prepend('<img class="plant" src="'+about_images[n]+'"  />')




				},
				allcomplete: function(e, ui) {

					load_contact();

				}
			});
		}
	}

	function load_location(){
		var el=$('#flag_location');
		var x=el.val();
		if(x==0){
			el.val(1);
			var location_images=new Array();
			location_images[0]='images/map.png';
			location_images[1]='images/tagabukid-bottom.png';
			$({}).imageLoader({
				images: location_images,
				async: false,
				complete: function( e, ui ) {
					var n=ui.i;
					if(n==0)	$('#location .map').css('background','url('+location_images[n]+') 0 0 no-repeat');
					if(n==1)	$('.land2').css('background','url('+location_images[n]+') center top no-repeat');
				},
				allcomplete: function(e, ui) {

					load_about();

				}
			});
		}
	}

	function load_home(){
		var el=$('#flag_home');
		var x=el.val();
		if(x==0){
			el.val(1);
			var img_home=($wh<750)?'-small':'';
			var home_images=new Array();
                        home_images[0]='images/bg-body.png';
                        home_images[1]='images/logo.png';
                        home_images[2]='images/badge.png';





                        home_images[3]='images/water-up.png';
                        home_images[4]='images/bottle-320ml.png';
			home_images[5]='images/splash.png';
                        home_images[6]='images/bg-home'+img_home+'.jpg';
                        home_images[7]='images/ph9.png';
                        home_images[8]='images/badge_sprite.png';
			home_images[9]='images/ph9_sprite.png';
//
//			home_images[0]='images/bg-body.png';
//			home_images[1]='images/bg-home'+img_home+'.jpg'
//			home_images[2]='images/splash.png';
//			home_images[3]='images/logo.png';
//			home_images[4]='images/badge.png';
//			home_images[5]='images/bottle-320ml.png';
//			home_images[6]='images/ph9.png';
//			home_images[7]='images/badge_sprite.png';
//			home_images[8]='images/ph9_sprite.png';

			$({}).imageLoader({
				images: home_images,
				async: false,
				complete: function( e, ui ) {
					var n=ui.i;
                                        if(n==0)	$('body').css('background','url('+home_images[n]+') 0 0 repeat-x');
                                        if(n==1)	$('.logo').css({'background':'url('+home_images[n]+') top center no-repeat','display': 'none'});
					if(n==2)	{
                                                            $('.logo #badge').css({'background':'url('+home_images[n]+') 0 0 no-repeat','display':'none'});
                                                            $('.logo, .logo #badge').fadeIn('slow');
                                                        }



                                                        
                                        if(n==3)        $('.water-up').append('<img id="water-up" alt="water" src="images/water-up.png" />').hide();






                                        if(n==4)        $('#home-slide').html('<img src="'+home_images[n]+'" />');
                                        if(n==5)        $('#splash').css({'background':'url('+home_images[n]+') center 0px no-repeat'}).css({'opacity':'0'});
                                        if(n==6)        {
                                                            $('#home').css({'background':'url('+home_images[n]+') center top repeat-x'});
                                                            $('.water-up').fadeIn(function(){
                                                                $('#splash').css({'background':'url('+home_images[5]+') center 0px no-repeat'}).animate({'opacity':'1'},'1',function(){
                                                                    $('#home-slide img').css({'display':'block'}); bottleup();
                                                                });
                                                            });
                                                        }
                                        
//                                        if(n==5)    
                                        
//                                        if(n==4)	$('#home').css({'background':'url('+home_images[n]+') center top repeat-x'});
//
//
//
//					if(n==5)	{
//                                                            $('#splash').css({'background':'url('+home_images[n]+') center 0px no-repeat'}).css({'opacity':'0'});
//////                                                            $('#home, #splash').fadeOut('slow');
//                                                        }
//                                        if(n==6)	{
//
//                                                           $('.water-up').fadeIn(function(){
//                                                                $('#home').css({'background':'url('+home_images[4]+') center top repeat-x'}).fadeIn('1',function(){
//                                                                    $('#splash').css({'background':'url('+home_images[5]+') center 0px no-repeat'}).animate({'opacity':'1'},'1',function(){
//                                                                    $('#home-slide').html('<img src="'+home_images[n]+'" />'); bottleup();
//                                                                });
//                                                                });
//
//                                                           });
//
//
//
//
//
//
//
//                                                        }
                                        if(n==7)	$('#ph9').html('<img src="'+home_images[n]+'" />');
                                        if(n==8)	$('#badge_sprite').css('background','url('+home_images[n]+') 0 0 no-repeat');
					if(n==9)	$('#ph9_sprite').css('background','url('+home_images[n]+') 0 0 no-repeat');
                                        
//					if(n==0)	$('body').css('background','url('+home_images[n]+') 0 0 repeat-x');
//					if(n==1)	$('#home').css('background','url('+home_images[n]+') center top repeat-x');
//					if(n==2)	$('#splash').css('background','url('+home_images[n]+') center 0px no-repeat');
//					if(n==3)	$('.logo').css('background','url('+home_images[n]+') top center no-repeat');
//					if(n==4)	$('.logo #badge').css('background','url('+home_images[n]+') 0 0 no-repeat');
//					if(n==5)	{$('#home-slide').html('<img src="'+home_images[n]+'" />'); bottleup();	}
//					if(n==6)	$('#ph9').html('<img src="'+home_images[n]+'" />');
//					if(n==7)	$('#badge_sprite').css('background','url('+home_images[n]+') 0 0 no-repeat');
//					if(n==8)	$('#ph9_sprite').css('background','url('+home_images[n]+') 0 0 no-repeat');
				},
				allcomplete: function(e, ui) {
					load_products();


				}
			});
		}
	}

	function load_products(){
		var el=$('#flag_products');
		var x=el.val();
		if(x==0){
			el.val(1);
			var product_images=new Array();
			product_images[0]='images/product_ad.jpg';
			product_images[1]='images/products/ns_purifiedwater.jpg';
			product_images[2]='images/products/ns_distilledwater.jpg';
			product_images[3]='images/products/ns_bubble-bottled-water.jpg';
			product_images[4]='images/products/ns_crystal-ice.jpg';
			product_images[5]='images/products/ns_5gallons.jpg';
			product_images[6]='images/products/ns_specialized-packaging.jpg';
			product_images[7]='images/products/ns_bai.jpg';

			$({}).imageLoader({
				images: product_images,
				async: false,
				complete: function( e, ui ) {
					var n=ui.i;
					if(n==0)	$('#product .prodbox').eq(0).prepend('<img class="" src="'+product_images[n]+'" style="margin:0;" />')
					if(n==1)	$('.prod-info').eq(0).before('<img class="" src="'+product_images[n]+'" />');
					if(n==2)	$('.prod-info').eq(1).before('<img class="" src="'+product_images[n]+'" />');
					if(n==3)	$('.prod-info').eq(2).before('<img class="" src="'+product_images[n]+'" />');
					if(n==4)	$('.prod-info').eq(3).before('<img class="" src="'+product_images[n]+'" />');
					if(n==5)	$('.prod-info').eq(4).before('<img class="" src="'+product_images[n]+'" />');
					if(n==6)	$('.prod-info').eq(5).before('<img class="" src="'+product_images[n]+'" />');
					if(n==7)	$('.prod-info').eq(6).before('<img class="" src="'+product_images[n]+'" />');


				},
				allcomplete: function(e, ui) {

					load_location();

				}
			});
		}

	}

	/*
	$('.loader').onImagesLoaded(function(_this){
		$(_this).fadeIn(1000, function(){

		});

	});

	*/

	$('body').scrollTo('#'+page, 800);




	/*= products */
	$('#product .nav > li > a').click(function(){
		$('#more-prodbox').stop().animate({bottom:'-950px'},400);
		$('.nav > li > ul > li > a').removeClass('current');
		return false;
	})
	$('#product-slider').codaSlider({dynamicTabs:false});


	$('.nav > li > ul > li > a').click(function(){

		$('.nav > li > ul > li > a').removeClass('current');
		$(this).addClass('current');

		$('#more-prodbox').stop().animate({bottom:'-950px'},400,function(){
			$(this).animate({bottom:0},600);
		})
		return false;
	})




	/*= home */

	$('#badge_sprite').sprite({fps: 9, no_of_frames: 50}).click(function(){
		if($(this).hasClass('svideo')){
			$(this).removeClass('svideo');
			$("#video_20th").fadeOut();
			$("#water-up, #home-slide, #ph9, #ph9_sprite").fadeIn();
			/*$('video, audio').each(function() {
				  $(this)[0].player.pause();
			});*/
		}else{
			$(this).addClass('svideo');
			$("#water-up, #home-slide, #ph9, #ph9_sprite").fadeOut();
			$("#video_20th").fadeIn();
		}
		return false;
	})

	$('#video_20th .close-btn').click(function(){
		$('#badge_sprite').removeClass('svideo');
		$("#video_20th").fadeOut();
		$("#water-up, #home-slide, #ph9, #ph9_sprite").fadeIn();
		/*$('video, audio').each(function() {
			  $(this)[0].player.pause();
		});*/
	})

	/*var img = new Image();
	$(img).load(function () {
		$(this).css('display','none');
		$('#home').css('background','url('+$(this).attr('src')+') center 0px repeat-x');

	}).error(function () {}).attr('src', 'images/bg-home.jpg');*/

	$('a[href="#home"]').click(function(){bottleup();})
	$('a[href="#space"]').click(function(){load_contact();})
	$('a[href="#about"]').click(function(){load_about();})
	$('a[href="#location"]').click(function(){load_location();})
	$('a[href="#product"]').click(function(){load_products();})


	load_home(); //loads home page first


	/*= contact*/
	$("#frmContact").submit(function(){
			$('div[id$="_err"]').html('');
			$.post('mailer.php', $("#frmContact").serialize(), function(data) {
				//alert(data)
				var d=$.trim(data);
				if(d!="sent" && d!="failed"){
					var err = data.split('|');
					if(err[0]==1) $('#name_err').html('is required');
					if(err[1]==1) $('#email_err').html('is required');
					if(err[2]==1) $('#email_err').html('is invalid');
					if(err[3]==1) $('#phone_err').html('is required');
					if(err[4]==1) $('#subject_err').html('is required');
					if(err[5]==1) $('#msg_err').html('is required');
				}else{
					if(d=='sent') alert("Message successfully sent.")
					else alert("There was an unexpected error while sending your message. Please try again later.")
				}
			});
			return false;


	})


	/*= about*/


	$('.kwicks').kwicks({ max : 630, easing: 'easeOutQuint'});

	$('#kwick1, #kwick2').unbind('hover').hover(function(){
		$(this).find('.plant').eq(0).stop().animate({left:0},200)
	},function(){
		$(this).find('.plant').eq(0).stop().animate({left:'-400px'},500)
	})

	$('#kwick3, #kwick4').unbind('hover').hover(function(){
		$(this).find('.plant').eq(0).stop().animate({left:0},200)
	},function(){
		$(this).find('.plant').eq(0).stop().animate({left:'-100px'},500)
	})

	$('#kwick5').unbind('hover').hover(function(){
		$(this).find('.plant').eq(0).stop().animate({left:0},200)
	},function(){
		$(this).find('.plant').eq(0).stop().animate({left:'-180px'},500)
	})




	$('#about .nav2 a').click(function(){
		$('#about .nav2 a').removeClass('current');
		$(this).addClass('current');

		$("#about-loader > div").slideUp();
		var id=$(this).attr('href');
		$(id).slideDown();
		return false;

	})

	$(window).scroll(function(){
		if(!isScrolledIntoView("#about-loader")){
			$("#about-loader > div").slideUp();
			$('#about .nav2 a').removeClass('current');
		}
	})



	/*= location */
	$("#locationMenu >li>ul>li>a").fancybox({
		'titlePosition'		: 'inside',
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'centerOnScroll'	: false
	});
	initLocationMenu();



	/*= global */
	$('.nav > li > a, #ph9_sprite').not('.news').click(function(){
		var a=$(this).attr('href');
		$('body').stop().scrollTo(a, 1500);
		return false;
	})
	$('.news').click(function(){document.location="news";})

	$('.fancybox').fancybox();

	function glow(){
		$('.glow').animate({'opacity' : "0"}, 3000, 'linear', function(){
			$('.glow').animate({'opacity' : "1"}, 3000, 'linear', glow());
		});
	}

	glow();


})


function initLocationMenu() {
	$('#locationMenu ul').hide();
	$('#locationMenu ul:first').show();
	$('#locationMenu >li>a').click(function() {
		var checkElement = $(this).next();
		if(checkElement.is(':visible')) {
			return false;
		}
		if(!checkElement.is(':visible')) {
			$('#locationMenu ul:visible').slideUp('normal');
			checkElement.slideDown('normal');
			return false;
		}
	});
}

function isScrolledIntoView(elem){
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();
    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

function bottleup(){
	var $wh=$(window).height();
	var minus=($wh<750)?70:0;
//	alert( minus)
	$('#home-slide img').stop().animate({top:700-minus},1000,function(){
		$(this).animate({top:130-minus},600,function(){
			$(this).animate({top:165-minus},500, function(){
				$("#ph9,#ph9_sprite").animate({bottom:190+minus, right:0}, 700, function(){
					$('#ph9_sprite').css('visibility','visible').sprite({fps: 9, no_of_frames: 50})
				});

			})

		})
	})
}
