var total_home_banner_li = 0;
var current_home_banner  = 1;
var set_int = 0;
var banner_cap_count = 0;

$(document).ready(function() {

	//popup
	$('a#news_link').popupWindow({ 
		height:600, 
		width:700, 
		top:50, 
		left:50 
	});

	//magic email
	$('a.mgceml').attr('href', 'mailto:'+ $('a.mgceml').attr('alt')	+'@'+ $('a.mgceml').attr('rel')).click(function() {
		$(this).html($('a.mgceml').attr('alt')	+'@'+ $('a.mgceml').attr('rel'));
																															
	});//click

	var banner_bolean = $('div#banner').length;

	if(!banner_bolean){
		$('a.current_page').addClass('current_page_alt').removeClass('current_page');	
	}


	$('div.wrap_entry hr').each(function(){
		$(this).replaceWith('<div class="hr">&nbsp;</div>');
	});



	var home_bolean = $('ul#home_banner_array').length;
	
	function change_home_banner(number) {
		if(banner_cap_count <= (total_home_banner_li*3)) {	
			if(number === undefined){ number = 'auto'; }
			//alert(number);
			if(current_home_banner > total_home_banner_li) {
				current_home_banner = 1;
			}
			if(number != 'auto'){
				current_home_banner = number;
			}
			
			$('ul#home_banner_array li a').removeClass('current_dot');
			//img
			var img = $('ul#home_banner_array li:nth-child('+current_home_banner+')').
					  find('a.copy_img_a').find('img').attr('src');
			var bg = "#ffffff url("+img+") center center repeat";
			$('div#home_banner').stop().animate({opacity: .5}, 500, function(){
				$('div#home_banner').css("background",bg);
				$('div#home_banner_bg').css("background-image","none");			
				$('div#home_banner').animate({opacity: 100}, 2500);
			});
			
			//copy
			var copy = $('ul#home_banner_array li:nth-child('+current_home_banner+')').
					   find('div.copy').html();
					   
			$('div#home_banner_copy').stop().animate({opacity: 0}, 500, function(){
				$('div#home_banner_copy').html(copy).animate({opacity: 100}, 3500);
				
				
			});
	
	//$('ul#home_banner_array li a').removeClass('current_dot');
			//alert(current_home_banner);
			$('ul#home_banner_array li:nth-child('+current_home_banner+')').find('a').
			addClass('current_dot');
			current_home_banner++;
			banner_cap_count++;
		}//if
	}
	
	if(home_bolean) {
		
		$('ul#home_banner_array li').each(function(i){
			total_home_banner_li++;
		});
		//alert('--');
		change_home_banner('auto');
		set_int = setInterval(change_home_banner, 7500, 'auto');
		// clearInterval(set_int);
		
		$("a.dots").click(function () {
			var rel = $(this).attr('rel');
			clearInterval(set_int);
			change_home_banner(rel);//alert(rel);
			return false;
		});

	}



	
	var start_with_bg = 0;
	
	//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: 'light_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").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){
		//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('a:first').attr('href', get_my_href);
			$(this).remove();
		}
	});
	
	
	// 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
	$('div#left_column img.copy_img_portrait').each(function(){
		
		$(this).load(function(){
			//$(this).pixastic("desaturate");
			var $this = $(this); 
			//var $w = $this.width();
			var $w = 39+(eval($this.css('marginRight').replace("px", ""))) + eval($this.width());
			//$b = $this.css('borderWidth');
			//alert($w);
			$this.parent().parent().find('p').css('marginLeft', $w);
		});
	});
	
	


	
});