//var root = 'http://www.sagebaggott.com/semibig/template02';
var nav_click_ready = true;


// OPEN SUB PAGES

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(400, function(){
		$('div'+page).find('ul.sub_nav').fadeIn(400);
	});	
	$("div.scroll_this").jScrollPane({
		showArrows: true,
		horizontalGutter: 10
	});
}

// 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();
	if(str=="" || str.indexOf(defaultText)!=-1){
		return true;
	}
	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;
		}
		
		//$('a.close').html(current_fading_image);
		$('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);

	$('div#home-block').animate({'opacity':'0','top':'0'},100);

	setTimeout(function(){
	
		$('div#intro').fadeOut('slow');
		
		var this_is = $('a#player_controls');
		player_controls(this_is);
		
		$('div#home-block').animate({'opacity':'1','top':'390'},1500,function(){
		
			//$('div#home-block').children().children().fadeTo(1000,1.0);
		
		});
		
	},2500);


	// SUB_NAV
	
	$('ul.sub_nav li a').click(function(){
	
		var this_is         = $(this);
		
		this_is.addClass('current').parent().siblings().find('a').removeClass('current');
		
		var this_page       = '#' + (this_is.closest('div.page').attr('id'));
		var sub_page_number = this_is.closest('li').attr('rel');  
		
		//alert(sub_page_number);
		//$('ul.sub_nav').animate({'opacity':'0'},50);
		open_sub_page(this_page, sub_page_number);
	
		return false;
	
	});

	// SUB NAV // CLOSE
	
	$('div.sub_page a.close').click(function(){
	
		$(this).closest('div.page').find('div.reopen').fadeTo('fast' ,0.9);
		reopen_sub_page = $(this).parent();
		reopen_xpos = reopen_sub_page.x;
		var width = $(this).parent().width();
		$(this).parent().animate({'right':'-'+width},500);
		$('ul.sub_nav').fadeOut('fast');
	
	});


	// SUB NAV // REOPEN
	
	$('div.reopen').click(function(){
		$(this).fadeOut('fast');
		reopen_sub_page.animate({'right':'0px'},500);
		$('ul.sub_nav').delay(600).fadeIn(400);
	
	});



	///////////  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();
		//alert(gallery_category);
	
	});

	///////////  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();
	});
	

	
	// fade in at begining

	$('div.center').fadeIn(2000, function(){
	
		fading_images();
	
	});
	
	
	
	
	// PAGE NAVIGATION  ///////////////////////////////////

	$('ul#main_nav li a').click(function(){
		if(nav_click_ready){
			var this_is = $(this);
			var class_is = $(this).attr('class');
			if(class_is != 'current'){
				nav_click_ready = false;	
				var page = $(this).attr('href');
				if(page == '#gallery'){
					setTimeout(function(){
						$('h1').animate({'margin-top':'30px'},1000);
					},500);		
				}
				$('ul#main_nav li a').removeClass('current');
				$('ul#main_nav li a[href="'+page+'"]').each(function(){
					$(this).addClass('current');
				});  
				$('div.page:visible').fadeOut(400, function(){
					$(this).find('div.sub_page').hide();
					// fade in new page
					$('div'+page).fadeIn(400, function(){
						// reset gallery and faading images
						fading_images();
						gallery_images();
						open_sub_page(page, '0');
						$('ul.sub_nav').hide();
						nav_click_ready = true;
					});			
				});		
			} // if
		}// 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: "#37342F",
			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);
		
		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   ///////////////////
	
	
	
	
	
	
	
	
	
	
	
	
	

});
