//GLOBAL VARIABLES
if(typeof(isIE6) == 'undefined'){var isIE6 = false;}

//DOM BEGINS
$(document).ready(function(){
//	preloadImages("../images",start);
//	start();
	//CALL PRELOADER
	preloader("../images",start);
	
	//PREVENT ENTER KEY FROM SUBMITTING FORM
	$(window).keypress(
		function(event){ 
			if (event.keyCode == 13) {
				event.preventDefault();
			} 
		}
	);
});
//DOM ENDS

//START
function start()
{
	$("body").load("index_txt.html", function(){
		//NAVIGATION
		nav();
		//SPORT JET BUTTON
		SJ();
		//FIRST LOAD
		loadContent("home");
	})
}
//NAVIGATION 
function nav()
{
	//HOVER EVENT
	$(".btn_navbar").hover(function(){
		var hovered = $(this).attr("id");
		lavaAnim(hovered);
	},function(){
		var current = $(".current").attr("id");
		lavaAnim(current);
	});
	//CLICK EVENT
	$(".btn_navbar").click(function(){
		var clicked = $(this).attr("id");
		//Removing .current
		$(".btn_navbar").removeClass("current");
		//Adding .current
		$("#" + clicked).addClass("current");
		//Slide it
		lavaAnim(clicked);
		//Load Content
		var page = clicked.substring(4,clicked.length);
		loadContent(page);
	});
}
//GENERATE GALLERY FUNCTION
function gallery(divID)
{
	var current = $(".current").attr("id").substring(4,$(".current").attr("id").length);
	if (current != "contact")
	{
		var str = "<ul id='sliderContent'>";
		var count = 0;
		$.each(imgGallery, function(){
			if (this.page == current)
			{
				str+="<li class='sliderImage'><img src='" + this.img + "' /><div class='bottom'>" + this.text + "</div></li>";
				count++;
			}
		});
		str+="<div class='clear sliderImage'></div></ul>";
		//PRINTING
		$("#" + divID).html(str);
		//STARTING ANIMATION
		if(count!=1)
		{
			$("#" + divID).s3Slider({timeOut: 4500});
		}
		else
		{
			$("#sliderContent .sliderImage").fadeIn(500, function(){
                            $(".bottom").slideDown(500, function(){
								faderStat = false;
							});
			});
		}
		//JUMP BUTTONS
		//CLICK EVENT
		$(".btn_jump").click(function(){
			var page = $(this).attr("id").substring(9,$(this).attr("id").length);
			//alert(page);
			$("#btn_" + page).trigger("click");
		});
	}
	else
	{
		generateMap();	
	}
}
//GENERATE MAP
function generateMap()
{
	//alert("MAP");
	$("#slider").gMap({ controls: true,
                  scrollwheel: true,
                  markers: [{ 	address:"24 Lexington Drive Bella Vista NSW 2153 Australia",
								html:"<b>Rising Stars</b><br>Suite 9A, 24 Lexington Drive<br>Baulkham Hills NSW 2153",
								popup:true,
								icon: {	image: "images/layout/favicon.png",
                                      	iconsize: [50, 50],
                                      	iconanchor: [5,5],
                                      	infowindowanchor: [5, 0] } 
							}],
                  zoom: 12 });
	
}
//LAVA ANIMATION FUNCTION
function lavaAnim(btn_id)
{
		var hovered = btn_id;
		var posHovered = 0;
		var counter = 0;
		$(".btn_navbar").each(function(){
			if($(this).attr("id") == hovered){posHovered = counter;}
			counter++;
		});

		if(!isIE6)
		{
			$(".nb_begin").stop().animate({"margin-left":posX_navbarButton[posHovered][0], "width":posX_navbarButton[posHovered][1]},{duration:700, complete:function(){}});
		}
		else
		{
			$(".nb_begin").stop().animate({"margin-left":posX_navbarButton_ie6[posHovered][0], "width":posX_navbarButton_ie6[posHovered][1]},{duration:700, complete:function(){}});
		}
}

//LOAD CONTENT FUNCTION
function loadContent(page)
{
	//CHANGING THE GALLERY
	$("#slider").html("&nbsp;");
	gallery("slider");
	Cufon.replace(".bottom", {fontFamily:"DIN"});

	//LOADING CONTENT
	$("#canva").load(page + "_txt.html", function(){
		//GOOGLE ANALYTICS
		_gaq.push(['_trackPageview', page + "_txt.html"]);

		basics();
	});
}
//BASICS
function basics()
{
		Cufon.replace(".subHeader_Text,.subText,.quote,.contact_col1,.contact_col2,.contact_col3,.contact_col4,.subTable", {fontFamily:"Oceania"});
		Cufon.replace(".subHeading,.subHeading2", {fontFamily:"DIN"});
		
		//FOOTER
		$(".fgoto").click(function(){
			var page=$(this).attr("rel");
			if (page!="contact"){loadContent(page);}
			else{$("#btn_contact").trigger("click");}
		});
		
		$(".logo").click(function(){
			//Removing .current
			$(".btn_navbar").removeClass("current");
			//Adding .current
			$("#btn_home").addClass("current");
			//Slide it
			lavaAnim("btn_home");
			//Load Content
			loadContent("home");
		});
		//iFIXPNG
		loadedCallbackFunction();
}

//FUNCTION SJ BUTTONS
function SJ()
{
	//HOVER EVENT
	$("#btn_SportJet").hover(function(){
		$(this).removeClass("btn_SJ").addClass("btn_SJ_hover");					
	},function(){
		$(this).removeClass("btn_SJ_hover").addClass("btn_SJ");
	});
	//CLICK EVENT
	$("#btn_SportJet").click(function(){
	//	win = window.open("","");
	//	win.location.href = "sportjet.html";
	
	//	window.open("http://www.sportjet.com.au");
		window.open("http://www.sportjet.com.au");
	//return false;
	});
}

//LOADEDCALLBACKFUNCTION - IFIXPNG
function loadedCallbackFunction(){	
	//FIX PNG IN IE6
	$('img').ifixpng();
	$('div').each(function(){
			if($(this).css("background-image").indexOf('png') != -1)
			{
				pngClass = '.' + $(this).attr("class");
				$("'" + pngClass + "'").ifixpng();	
			}
	});
}

