var pos_i = 0;
var home_i = 0;
var rotate_i = 0;
//$('body').hide();

$(document).ready(function() {


	$('a#mind_body').click(function(){
	
		$('div#choose_location_dialog').show();
		return false;
	
	});
	
//	$('a#mind_body').prettyPhoto({
	//	animationSpeed: 'normal', /* fast/slow/normal */
	//	padding: 40, /* padding for each side of the picture */
	//	opacity: 0.35, /* Value betwee 0 and 1 */
	//	showTitle: true, /* true/false */
	//	allowresize: true, /* true/false */
	//	counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
	//	theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
	//	hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
	//	modal: false, /* If set to true, only the close button will close the window */
	//	changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
	//	callback: function(){} /* Called when prettyPhoto is closed */
//	});




	function adjust_nav(){
		var ww = $('div#bar_wrapper').width();
		//$("#testimonials").html(ww);
		if(ww < 1111){
			//alert('small');
			$('div#bar').addClass('bar_smaller');
			$('a#esd').addClass('jump_up');
			$('div#reg').addClass('reg_left');
		}
		else {
			$('div#bar').removeClass('bar_smaller');
			$('a#esd').removeClass('jump_up');
			$('div#reg').removeClass('reg_left');
		}
	}

	$(window).bind("resize", resizeWindow);
	function resizeWindow( e ) {
		adjust_nav();
	}

	adjust_nav();




	function animateBackground() {
		
		if(pos_i < 200){
			pos_i = pos_i + 2;
			var bp = 'right -'+pos_i+'px';
			//alert(bp);
			$('div#grundge').css({'background-position':bp });
		}
	}
	
	

	function stallthis() {
	
		$('div#grundge').css({background: 'transparent url(/assets/img/grundge_bg.png) right top repeat-y'});
		$('div#ken_icon').css({background: 'transparent url(/assets/img/ken.png) -70px 10px no-repeat fixed'});
			
		setInterval(animateBackground, 100);
	
	}
	
	setTimeout(stallthis, 2000);


	$("#newsletter_button").click(function () { 
    	//alert('--');
		//var k = 1;
		$('div#newsletter_div input').each(function(k){
			var type = $(this).attr('type');
			//alert(hidden);
			if(type == 'text'){
				$(this).addClass('SignUp_input');
			}
			else if(type == 'submit'){
				$(this).addClass('SignUp_button');
			}
		});
		$('div#newsletter_div').slideDown('slow');
		return false;
    });



	$('.only_sign_up').hide();
	$('.only_contact').hide();

	$('div#left_column h1').remove();
	$('div#ken_n_steph').animate({left: '250%'},10).show()
	.animate({left: '50%'}, 3000, 'swing');
	
	var start_with_bg = 0;
	
	function rotate_background() {
		
		if(start_with_bg >= 3){
				start_with_bg = 0;
		}
		if(start_with_bg == 0){
			var bg = "transparent url('/assets/img/ken_n_steph.png') 0 bottom no-repeat";
		}
		else if(start_with_bg == 1){
			var bg = "transparent url('/assets/img/girl.png') -40px bottom no-repeat";
		}
		else if(start_with_bg == 2){
			var bg = "transparent url('/assets/img/logo_large.png') 0 center no-repeat";
		}
		
		$('div#ken_n_steph').animate({opacity: 0}, 1000, function(){
				$('div#ken_n_steph').css({background: bg}).animate({opacity: 100}, 3000);								
		});
		
		start_with_bg++;
		home_i++;
		//alert(home_i);
		if(home_i >= 7) {
			clearInterval(rotate_i);
		}
	}
	
	rotate_i = setInterval(rotate_background, 15000);
	
	
	
	
	//takes class name from timy mce and adds the rel for prettyPhoto
	//$('.youTube').attr('rel', 'prettyPhoto[flash]'); 
	$('a.youTube').each(function(i){
		
		$(this).attr('rel', 'prettyPhoto[flash]');
		var current_url = $(this).attr('href');
		//alert(current_url);
		var str_array = current_url.split("v=");
		//alert(str_array[1]);
		var new_url = "/assets/swf/video.swf?width=550&height=400&v=" + str_array[1];
		//alert (new_url);
		////$(this).attr('href', new_url);
		
		// change image link
		$(this).parent().parent().find('img.copy_img').parent().attr('rel', 'prettyPhoto[flash][1]')
			.attr('href', new_url);
		//alert(whatisimage);
	});
	
	
	
	
	
	
	
	
	
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
				animationSpeed: 'normal', /* fast/slow/normal */
				padding: 40, /* padding for each side of the picture */
				opacity: 0.35, /* Value betwee 0 and 1 */
				showTitle: true, /* true/false */
				allowresize: true, /* true/false */
				counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
				theme: 'dark_square', /* light_rounded / dark_rounded / light_square / dark_square */
				hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
				modal: false, /* If set to true, only the close button will close the window */
			//	changepicturecallback: function(){}, /* Called everytime an item is shown/changed */
				callback: function(){} /* Called when prettyPhoto is closed */
			});


	//

	$('a.audio').each(function(){
				
		var new_audio = $(this).attr('href');

		var player = '<object type="application/x-shockwave-flash" data="http://www.macloo.com/examples/audio_player/player.swf" id="audioplayer1" class="audio_player" height="24" width="290"><param name="movie" value="http://www.macloo.com/examples/audio_player/player.swf"><param name="FlashVars" value="playerID=audioplayer1&soundFile='+new_audio+'"><param name="quality" value="high"><param name="menu" value="false"><param name="wmode" value="transparent"></object><p><small><a href="'+new_audio+'">Download mp3</a></small></p>'; 

		$(this).before(player);				 
		$(this).hide();				 
							 
	});
	
	
	
	$("div#contact_form_inner").hide();
	
	function open_close_contact_form(thisis){
		//alert('open_close_contact_form');
		var whatrel = $("div#contact_form_inner").attr('rel');
		var thisrel = thisis.attr('rel');
		//alert(thisrel);
		//if(whatrel == "closed"){
			$("div#contact_form_inner").slideDown('slow', function(){
																   
				//$('body').animate({scrollTop:0}, 'slow');
				if(thisrel == 'sign_up') {
					$('.only_sign_up').slideDown('fast');
					$('.only_contact').hide();
					//$("text#message").html('Sign me up for the 3 day free trial.');
					$("input#type").val('sign_up');
				}
				else {
					$('.only_sign_up').hide();
					$('.only_contact').slideDown('fast');
					$("input#type").val('contact');
				}
				
				$('html').animate({scrollTop:0}, 'slow'); 
				$('body').animate({scrollTop:0}, 'slow'); 
				
				$('div#ken_n_steph').animate({left: '250%'},2000);
		
		
		/*		
				sIFR.replace(specialfont, {
				selector: 'div#contact_form_div p.contact_heading',
				css: '.sIFR-root { color: #a1a1a0; text-transform:none; font-size: 35px;}'
				});
				
		*/
				
			}).attr('rel', 'opened');
			
					//$('body').animate({scrollTop:0}, 'slow'); 


		//}
		/* else {
			$("div#contact_form_inner").slideUp('slow').attr('rel', 'closed');
			$('html').animate({scrollTop:0}, 'slow'); 
			$('div#ken_n_steph').animate({left: '50%'},2000);

		} */
	}
	
	
	$("a.contact_open_button, #new_home_banners .join").click(function () { 
    	var thisis = $(this);
		open_close_contact_form(thisis); 
		return false;
    });
	
	$("textarea.c_multi").focus(function () {
         $(this).animate({height: 150}, 'slow');
    });




	// remove index
	$('li.headlink ul li').each(function(i){

		//alert(i);
		var this_a_html = $(this).find('a').html();
		//var dontreassign = $(this).attr('class');
		//alert(this_a_html);
		
		if(this_a_html == 'Index' || this_a_html == 'index') {
			var get_my_href = $(this).find('a').attr('href');
			//alert(get_my_href);
			$(this).parent().parent().find('a:first').attr('href', get_my_href);
			$(this).remove();
		}
	});
	
	$('a.kens_site').attr('href','http://www.sgtken.com');
	
	// remove index
	$('ul.sibling_ul li').each(function(i){
		//console.log(i);
		var this_a_html = $(this).find('a').html();
		
		if(this_a_html == 'Index' || this_a_html == 'index') {
			var get_my_href = $(this).find('a').attr('href');
			$(this).parent().parent().find('h3:first a').attr('href', get_my_href);
			$(this).remove();
		}
	
	
	});
	
	// shift P over to line up with image right
	$('img.copy_img_portrait').each(function(){
		
		//$(this).pixastic("desaturate");

		
		var $this = $(this); 
		//var $w = $this.width();
		var $w = 20+(eval($this.css('marginRight').replace("px", ""))) + eval($this.width());
		//$b = $this.css('borderWidth');
		//alert($b);
		$this.parent().parent().find('p').css('marginLeft', $w);
	});
	
	
	
	
	// NEW HOME BANNERS
	$('div#new_home_banners a.tabs').click(function(){
	
		var this_rel = $(this).attr('rel');
		//alert(this_rel);
		
		if(this_rel == '1'){
		
			$('div#new_home_banner_1').fadeIn();
			$('div#new_home_banner_2').fadeOut();
			$('div#new_home_banner_3').fadeOut();
		
		}
		
		else if(this_rel == '2'){
		
			$('div#new_home_banner_1').fadeOut();
			$('div#new_home_banner_2').fadeIn();
			$('div#new_home_banner_3').fadeOut();
		
		}
		
		else if(this_rel == '3'){
		
			$('div#new_home_banner_1').fadeOut();
			$('div#new_home_banner_2').fadeOut();
			$('div#new_home_banner_3').fadeIn();
				
		}
		
	
	});
	
	
		
	function preload(arrayOfImages) {
	    $(arrayOfImages).each(function(){
	        $('<img/>')[0].src = this;
	        // Alternatively you could use:
	        // (new Image()).src = this;
	    });
	}
	
	// Usage:
	
	preload([
	    '/assets/img/home_banner_tab_1.png',
	    '/assets/img/home_banner_tab_2.png',
	    '/assets/img/newsletter.jpg',
	    '/assets/img/home_banner_tab_3.png'
	]);
	


	
});
