var init;
function setUpHome() {
	setUpEmail();
	setUpHomeSlider();
}

var isPaused = false;
function setUpHomeSlider() {
	$("#slideshow .paging").show();
	$("#slideshow .slidemenu a:first").addClass("active");
	var imageWidth		= $("#slideshow .window").width();
	var imageSum 		= $("#slideshow .image_reel li").size();
	var imageReelWidth	= imageWidth * imageSum;

	//slidemenu 
	for(i=1; i <= imageSum; i++) {
		$(".slidemenu").append('<a href="#" class="lnk" rel="'+i+'">&bull;</a>');
	}
	$(".slidemenu a:first-child").addClass('active');
	
	$("#slideshow .image_reel").css({'width' : imageReelWidth});
	if(imageSum < 2) {
		$("#slideshow .paging").hide();
		return;
	}
	
	rotate = function(){
		var triggerID = $active.attr("rel") - 1;
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$("#slideshow .slidemenu a").removeClass('active');
		$active.addClass('active');
		$("#slideshow .image_reel").animate({left: -image_reelPosition}, 500 );
	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ 
			$active = $('.slidemenu a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.slidemenu a:first'); //go back to first
			}
			rotate(); 
		}, 15000);
	};

	//On Hover
	$(".image_reel li").hover(function() {
		clearInterval(play); 
		}, function() {
			if(isPaused == false) { rotateSwitch(); }
		}
	);	
	
	//On Click
	$(".slidemenu a").click(function() {
		$active = $(this); //Activate the clicked paging
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
	
	//PauseButton
	$(".paging .pause").click(function() {
		if(isPaused != false) {
			rotateSwitch();
			$(this).css({'backgroundPosition' : "-88px  0"});
			$(this).hover(function() {$(this).css({'backgroundPosition' : "-110px  0"});}, function() {$(this).css({'backgroundPosition' : "-88px  0"});});
		} else {
			clearInterval(play); 
			$(this).css({'backgroundPosition' : "-132px 0"});
			$(this).hover(function() {$(this).css({'backgroundPosition' : "-154px  0"});}, function() {$(this).css({'backgroundPosition' : "-132px  0"});});
		}
		isPaused = !isPaused;
	});
	
	rotateSwitch();
}

function setUpEmail(){
	$("#emailform").focus(function() {
		if($("#emailform").val() == 'Enter Your Email Address') {
			$("#emailform").val('');
		}
	});
	$("#emailform").blur(function() {
		if($("#emailform").val() == '') {
			$("#emailform").val('Enter Your Email Address');
		}
	});
	$("#emailformdb").submit(function() {  
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = new String($("#emailform").val());
		if (email.search(reg) != 0) {
			$(".homeemailerror").html('Invaild Email Address');
			$(".homeemailmessage").hide();
			$(".homeemailerror").show();
			init = setInterval("showEmailThing()", 3000);
			return false;
		}
		var dataString = 'email='+ email + '&byway=1';
		//alert (dataString);return false;
		
		$.ajax({ type: "POST", url: "emailprocess/", data: dataString,
		success: function(data) {
			if(data == '') { data = 'form is submitted'; }
			$(".homeemailerror").html(data);
			$(".homeemailmessage").hide();
			$(".homeemailerror").show();
			init = setInterval("showEmailThing()", 6000);
			}
		});	
		return false;
	});  
}

function showEmailThing() {
	$(".homeemailmessage").show();
	$(".homeemailerror").hide();
}

function setUpVideoPopUp() {
	if ($("#background").length == 0 ) {
		$("body").append('<div id="background"></div>');
		$('#background').css('height',($('body').height()+40)+"px");
	}
	if ($("#videopopp").length == 0 ) {
		$("body").append('<div id="videopopp"><div id="thevideo"></div><div id="closebox"><a id="closer" href="javascript:closevideo();"></a></div></div>');
	}
	
	$('#background').click(function() {
		closevideo();
	});
	
	$('a[rel*=popup]').click(function() {
		$("#videopopp").css({width:'720px',height:'450px'});
		$("#thevideo").css({width:'720px',height:'450px'});
		$("#closer").css({top:'-483px'});
	
		$("#videopopp").center();
		$("#background").fadeIn();
		$("#videopopp").fadeIn();
			
		$linkto = $(this).attr("href");
		$("#thevideo").html('<iframe type="text/html" width="100%" height="100%" src="'+$linkto+'" frameborder="0"/>');
		return false;
	});
	
	$('a[rel*=popup2]').click(function() {
		$("#videopopp").css({width:'950px',height:'550px'});
		$("#thevideo").css({width:'950px',height:'550px'});
		$("#closer").css({top:'-583px'});
		
		$("#videopopp").center();
		$("#background").fadeIn();
		$("#videopopp").fadeIn();
		
		$linkto = $(this).attr("href");
		$("#thevideo").html('<div style="background:#fff; height:100%;"><iframe type="text/html" width="100%" height="100%" src="'+$linkto+'" frameborder="0"/></div>');
		return false;
	});
}

function setUpMapPopUp() {
	if ($("#background").length == 0 ) {
		$("body").append('<div id="background"></div>');
		$('#background').css('height',($('body').height()+40)+"px");
	}
	if ($("#videopopp").length == 0 ) {
		$("body").append('<div id="videopopp"><div id="thevideo"></div><div id="closebox"><a id="closer" href="javascript:closevideo();"></a></div></div>');
		$('#videopopp').css('width',"620px");
	}
	
	$('#background').click(function() {
		closevideo();
	});
	
	$('a[rel*=popup]').click(function() {
		$("#videopopp").css({width:'720px',height:'450px'});
		$("#thevideo").css({width:'720px',height:'450px'});
		$("#closer").css({top:'-483px'});
		
		$("#videopopp").center();
		$("#background").fadeIn();
		$("#videopopp").fadeIn();
		
		$linkto = $(this).attr("href");
		$class  = $(this).attr("class");
		if($class == 'specials') {
			$v = $(this).parent().children(".spopip");
			//alert($v.html());
			$("#thevideo").html('<div class="spopiptxt">'+$v.html()+'</div>');
		} else {
			$("#thevideo").html('<img src="'+$linkto+'" alt="Map" border="0"/>');
		}
		return false;
	});
}

function closevideo() {
	$("#thevideo").html('');
	$("#videopopp").fadeOut();
	$("#background").fadeOut();
}

jQuery.fn.center = function () {
	this.css("position","absolute");
	het = ($(window).height() - this.height() ) / 2+$(window).scrollTop() -5;	
	if(het > 0) {
		this.css("top", het+"px");
	} else {
		this.css("height",($(window).height()+$(window).scrollTop() -5) +"px");
	}
	this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
	return this;
}


$(document).ready(function() {
	
	setUpVideoPopUp() ;
});
