
var reopen_this = '';
var nav_click_ready = true;

function open_sub_page(page, sub_page_number){

	$('div'+page).find('ul.sub_nav li:eq('+sub_page_number+') a')
		.addClass('current')
		.parent()
		.siblings()
		.find('a')
		.removeClass('current');
	
	$('div.page').find('div.sub_page').fadeOut();
	$('div'+page).find('div.sub_page:eq('+sub_page_number+')').fadeIn('slow');	
	$("div.scroll_this").jScrollPane({
		showArrows: true,
		horizontalGutter: 10
	});
	if(page=="#home"){
		$("div"+page+" div#home_content").fadeIn('slow');
	}
}


// check if an element is empty or contains default text
// @param element (String): element that we are searching in String format
// @param defaultText (String): default text string to search for in addition to empty string
function checkIfEmpty(element, defaultText){
	// check if there's content in the element
	var str = $(element).text();
	//console.log("str: " + str);
	if(str=="" || str.indexOf(defaultText)!=-1){
		//console.log("empty or default text");
		return true;
	}
	//console.log("contains non-default text");
	return false;
}



/*////////////////////    COMMON GALLERY FUNCTIONS   //////////////////////*/

// fading images
var fading_images_int;
var total_fading_images = 0;
var current_fading_image = 0;

function fading_images(){

	clearInterval(fading_images_int);
	total_fading_images = $('div#home ul.fading_images li').length;
	current_fading_image = 0;
	
	$('div#home ul.fading_images li:eq('+current_fading_image+')').fadeIn(1000);
	$('div#home ul.fading_images li:eq('+current_fading_image+')').siblings().fadeOut(1000);
	
	fading_images_int = setInterval(function(){
		current_fading_image++;
		if(current_fading_image >= total_fading_images){
			current_fading_image = 0;
		}
		$('div#home ul.fading_images li:eq('+current_fading_image+')').siblings().fadeOut(1000);
		$('div#home ul.fading_images li:eq('+current_fading_image+')').fadeIn(1000);
	}, 5000);
	
} // fading_images


// gallery images
var gallery_category = 'all';
var gallery_images_int;
var total_gallery_images = 0;
var current_gallery_image = 0;

function gallery_images(){

	clearInterval(gallery_images_int);

	if(gallery_category == 'all'){
		total_gallery_images = $('div#gallery ul.gallery_images li').length;
		$('div#gallery ul.gallery_images li').each(function(i){
		
			$(this).attr('rel',i).addClass('active');
		
		});//.addClass('active');
	}
	else {
		total_gallery_images = $('div#gallery ul.gallery_images li[scope="'+gallery_category+'"]').length;
		$('div#gallery ul.gallery_images li').removeClass('active').attr('rel','');
		$('div#gallery ul.gallery_images li[scope="'+gallery_category+'"]').each(function(i){
		
			$(this).attr('rel',i).addClass('active');
		
		});//.addClass('active');
	}
	
	$('span#gallery_total').html(total_gallery_images);

	current_gallery_image = 0;

	//alert(current_gallery_image);
	$('span#gallery_current').html(current_gallery_image+1);

	$('div#gallery ul.gallery_images li[rel="'+current_gallery_image+'"]').fadeIn(1000);
	$('div#gallery ul.gallery_images li[rel="'+current_gallery_image+'"]').siblings().fadeOut(1000);
	
	gallery_images_int = setInterval(function(){
	
		current_gallery_image++;
		animate_gallery_images();
	
	}, 5000);
	
	gallery_caption(current_gallery_image);
		
}

function animate_gallery_images(){

	if(current_gallery_image >= total_gallery_images){
		current_gallery_image = 0;
	}
	if(current_gallery_image < 0){
		current_gallery_image = total_gallery_images-1;
	}
	
	$('span#gallery_current').html(current_gallery_image+1);

	$('div#gallery ul.gallery_images li[rel="'+current_gallery_image+'"]').fadeIn(1000);
	$('div#gallery ul.gallery_images li[rel="'+current_gallery_image+'"]').siblings().fadeOut(1000);

	gallery_caption(current_gallery_image);
	
}

function start_gallery(){

	clearInterval(gallery_images_int);

	gallery_images_int = setInterval(function(){
	
		current_gallery_image++;
		animate_gallery_images();
	
	}, 5000);
	
	$('span#start_slide_show').hide();
	$('span#pause_slide_show').show();

}

function gallery_caption(current_gallery_image){
	var gallery_caption = $('div#gallery ul.gallery_images li[rel="'+current_gallery_image+'"]').attr('title');
	if(gallery_caption == undefined) gallery_caption = '';
	if(gallery_caption == ''){
		$('div#gallery_captions').fadeOut('fast');
	}
	else {
		$('div#gallery_captions').fadeIn('fast').html('<p>'+gallery_caption+'</p>');
	}
}

/*////////////////////    /COMMON GALLERY FUNCTIONS   //////////////////////*/

function console_log(foo){

	//console.log(foo);
	
}


// //////////////////////////  DOCUMENT READY  ////////////////////////// // 

$(document).ready(function(){
	// fade in intro
	setTimeout(function(){
		$('div#intro h2, div#intro h3').fadeIn('fast');
	},500);

	setTimeout(function(){
		$('div#intro').fadeOut('slow');
		var this_is = $('a#player_controls');
		player_controls(this_is);

		if(checkIfEmpty("div#home div.scroll_this", "Edit me")!=true){
			open_sub_page('#home', '0');
			$("div#home_content").delay(1000).animate({'left':'200', 'opacity':'1'},500, function(){

			});
			$('div#home div.scroll_this').jScrollPane({
				showArrows: true,
				horizontalGutter: 10	
			});
		}else{
			if(logged_in){ // if there's no content, but you're logged in, also show the home sub page
				open_sub_page('#home', '0');
				$("div#home_content").delay(1000).animate({'left':'200', 'opacity':'1'},500, function(){

				});
				$('div#home div.scroll_this').jScrollPane({
					showArrows: true,
					horizontalGutter: 10	
				});
			}
		}
		$("div#social").fadeIn(500);
		$("ul.fading_images").fadeIn(500);
		
	},2500);

	// fade in at begining
	$('div#header, div#content, div#footer').fadeIn(1000, function(){
		fading_images();
	});

	// SUB_NAV
	$('ul.sub_nav li a').click(function(){
		var this_is         = $(this);
		$(this).addClass('current').siblings().removeClass('current');
		
		var this_page       = '#' + (this_is.closest('div.page').attr('id'));
		var sub_page_number = this_is.closest('li').attr('rel');  
		open_sub_page(this_page, sub_page_number);
		
		return false;
	});
	
	// From template 04, works better than above
	// $('div.wrap_subs_nav a').click(function(){
	// 
	// 	var this_rel = $(this).attr('rel');
	// 	$(this).addClass('selected').siblings().removeClass('selected');
	// 	
	// 	$(this).closest('div.inner').find('div.wrap_subs div[rel="'+this_rel+'"]').show().jScrollPane({
	// 				
	// 					showArrows: true,
	// 					horizontalGutter: 10
	// 				
	// 			}).siblings().hide();
	// 	
	// });
	

	// SUB NAV // CLOSE	
	$('div a.close').click(function(){
		var width = $(this).parent().width();
		//$(this).parent().animate({'left':'-'+width, 'opacity':'0'},500);
		$(this).parent().fadeOut('slow');
		// show .reopen
		$(this).parent().parent().find('.reopen').fadeIn('slow');
		reopen_this = $(this).parent();
	});
	
	
	// REOPEN //
	$('div.reopen').click(function(){
		$(this).fadeOut('slow');
		//reopen_this.animate({'left':'200','opacity':'1'},600);
		reopen_this.fadeIn('slow');
	});

	///////////  GALLERY
	// gallery categories // scope should match that of the banner lis
	$('div#gallery ul.gallery_categories li').click(function(){
		gallery_category = $(this).attr('scope');
		$(this).addClass('active');
		$(this).siblings().removeClass('active');
		gallery_images();
		start_gallery();
	});

	///////////  GALLERY   // START SLIDE SHOW

	$('span#start_slide_show').click(function(){
		current_gallery_image++;
		animate_gallery_images();
		$('span#start_slide_show').hide();
		$('span#pause_slide_show').show();		
		gallery_images_int = setInterval(function(){	
			current_gallery_image++;
			animate_gallery_images();	
		}, 5000);
	});
	
	///////////  GALLERY   // PAUSE SLIDE SHOW
	
	$('span#pause_slide_show').click(function(){
	
		clearInterval(gallery_images_int);
		$('span#start_slide_show').show();
		$('span#pause_slide_show').hide();
	});


	///////////  GALLERY   // ARROWS
	
	$('a#gallery_arrow_left').click(function(){
		current_gallery_image--;
		animate_gallery_images();
		clearInterval(gallery_images_int);
		$('span#start_slide_show').show();
		$('span#pause_slide_show').hide();
	});
	$('a#gallery_arrow_right').click(function(){
		current_gallery_image++;
		animate_gallery_images();
		clearInterval(gallery_images_int);
		$('span#start_slide_show').show();
		$('span#pause_slide_show').hide();
	});

	

	
	// PAGE NAVIGATION
	
	$('ul#main_nav li a').click(function(){
		if(nav_click_ready){
			var class_is = $(this).attr('class');
			if(class_is != 'current'){	
				nav_click_ready = false;
				$('div.reopen').fadeOut('fast');
				var page = $(this).attr('href');
				// $('div.scroll_this').jScrollPane({
				// 	showArrows: true,
				// 	horizontalGutter: 10	
				// });
				$('ul li a.current').removeClass('current');
				$('div.page').find('div.sub_page').fadeOut();
				$('div.page:visible').fadeOut(1500, function(){
					// fade out old sub-page
					
					// fade in new page
					$('div'+page).fadeIn(1500, function(){
						// reset gallery and faading images
						fading_images();
						gallery_images();
						// if(checkIfEmpty("div" +page+ " div.scroll_this", "Edit me")==false){
						// 	open_sub_page(page, 0)
						// }else{
						// 	if(logged_in){ // if there's no content, but you're logged in, also show the home sub page
						// 		open_sub_page(page, 0);
						// 	}
						// }
						if(page=="#home"){
							if(checkIfEmpty("div" +page+ " div.scroll_this", "Edit me")==false){
								open_sub_page(page, 0)
							}else{
								if(logged_in){ // if there's no content, but you're logged in, also show the home sub page
									open_sub_page(page, 0);
								}
							}
						}else{
							open_sub_page(page, '0');
						}
						
						nav_click_ready = true;
					});			
				});
			} // if		
			$(this).addClass('current');
		}// if
		return false;		
	});
	
	
	///////////////////   **NEW**  MUSIC PLAYER   ///////////////////

	$("#jquery_jplayer_1").jPlayer({
	        ready: function () {
	          $(this).jPlayer("setMedia", {
	            mp3: "assets/player/song.mp3"
	          });
	        },
	        swfPath: "assets/js",
	        supplied: "mp3",
			cssSelectorAncestor: "",
			backgroundColor: "#FFF",
			cssSelector: {
			        play: "",
			        pause: "",
			        stop: "",
			        videoPlay: "",
			        seekBar: "",
			        playBar: "",
			        mute: "",
			        unmute: "",
			        volumeBar: "",
			        volumeBarValue: "",
			        currentTime: "",
			        duration: ""
			      }
		});
		
	$('a#player_controls').click(function(){
		player_controls($(this));
		return false;
	});
	
	function player_controls(this_is){
		//scope is used to remember the current state // "0"=off or "1"=on
		var scope = this_is.attr('scope');
		if(scope == '0'){
			//make it play
			this_is.html('jp-pause').attr('scope','1').addClass('jp-pause').removeClass('jp-play');
			// jPlayer play
			$("#jquery_jplayer_1").jPlayer("play");
		}else if(scope == '1'){
			//make it pause
			this_is.html('jp-play').attr('scope','0').addClass('jp-play').removeClass('jp-pause');
			// jplayer pause
			$("#jquery_jplayer_1").jPlayer("pause");
		}
	} //player_controls
	
	
	
	///////////////////   FORM   ///////////////////

	$('.replaceDefaultText').each(function(i){
	
		var this_is = $(this);
		
		this_is.focus(function(){
		
			var initial_val = this_is.attr('rel');
			var current_val = this_is.val();
			
			if(current_val == initial_val){
			
				this_is.val('');
			
			}
		
		}); // focus
		
		this_is.blur(function(){
		
			var initial_val = this_is.attr('rel');
			var current_val = this_is.val();
			
			if(current_val == ''){
			
				this_is.val(initial_val);
			
			}
			
		}); // blur
		
	});
	
	$('div.jspDrag').live('mouseenter',function(){
	
		$(this).animate({'left':'-10px'},250);
	
	});
	
	$('div.jspDrag').live('mouseout',function(){
	
		//setTimeout(function(){
		
			$(this).stop().animate({'left':'0px'},250);
		
		//},250);
	
	});
	
	// validate eamil
	function validate(address) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	   //var address = document.forms[form_id].elements[email].value;
	   if(reg.test(address) == false) {
	      //alert('Invalid Email Address');
	      return false;
	   }
	   else { return true; }
	}


	// check form for errors
	function check_for_errors(this_form){
	
		// error checking
		var errors = 0;
		//var serialize_ = $(this).serialize();	
		
		// clean up error class
		$('label.error').remove();
		
		// check for blank requires fields
		this_form.find('.required').each(function(i){
			$(this).removeClass('input-error');
			var this_val = $(this).val();
			var this_rel = $(this).attr('rel');
			var this_err = $(this).attr('scope');
			if(this_val == '' || this_val == this_rel){
				$(this).addClass('input-error').before('<label class="error">'+this_err+'<span></span></label>');
				errors++;
			}
		});
		// test email
		this_form.find('.email').each(function(i){
			$(this).removeClass('input-error');
			var this_val = $(this).val();
			var this_rel = $(this).attr('rel');
			var this_err = $(this).attr('scope');
			var valid_email = validate(this_val);
			
			if(valid_email == false || this_val == this_rel){
				$(this).addClass('input-error').before('<label class="error">'+this_err+'<span></span></label>');
				errors++;
			}
			
		});
		
		return errors;
	}
	
	// FORM HANDLER
	
	$('form#contact_form').submit(function(){
	
		var this_form = $(this);
		var has_errors = check_for_errors(this_form);
		//alert(has_errors);
		
		if(!has_errors){
		
			var form_values = this_form.serialize();
			
			$.ajax({
					type: "POST",
					url: 'assets/ajax/form_handler.php',
					data: form_values,
					success: function(msg){
												
						alert(msg);
						
					} // success
					
			}); //ajax
			
		} // if
								
		return false;
	
	});
	
	// run each time a user leaves the input field
	$('form .input-error').live('blur',function(){
	
		var this_form = $(this).closest('form');
		check_for_errors(this_form);
	
	});
	
	$('input[type="reset"]').click(function(){
	
		$('label.error').remove();
		$('form .required').removeClass('input-error');
	
	});
	
	///////////////////   */FORM   ///////////////////


});
