$(document).ready(function(){
	$(document).pngFix();
	
	active = $(".navi1 .active");
	active.prev().empty();
	active.next().empty();
	
	var bar1, bar2;
	mainNavi();

	fadeBanner( 3000 );
	fontSize();
	queryForm();
	mailingList();
	slideLocation();
	storiesHover();
});


function mainNavi() {
	$(".navi1 a").hover(
		function() {
			bar1 = $(this).prev().contents().detach();
			bar2 = $(this).next().contents().detach();
		},
		function() {
			bar1.appendTo($(this).prev());
			bar2.appendTo($(this).next());
		}
	)
}

function fontSize() {
	size = getCookie( "widhh-font" );
	if ( size == "L" ) {
		$(".large").addClass("bb");
		$(".medium").removeClass("bb");
		$(".size_change").css("font-size", "1.2em");
	}
	
	$(".large").click( function(e) {
		e.preventDefault();
		$(this).addClass("bb");
		$(".medium").removeClass("bb");
		$(".size_change").css("font-size", "1.2em");
		setCookie( "widhh-font", "L", 30 );
	});

	$(".medium").click( function(e) {
		e.preventDefault();
		$(this).addClass("bb");
		$(".large").removeClass("bb");
		$(".size_change").css("font-size", "1em");
		setCookie( "widhh-font", "M", 30 );
	});
}

function queryForm() {
	$(".query").focus( function() {
		$(this).data( 'value', $(this).val() ).val('');
	});
	$(".query").blur( function() {
		if ( $(this).val() == '' )
			$(this).val( $(this).data('value') );
	});
}

function mailingList() {
	$(".mailing_form").submit(function(e) {
		str = $("input:first").val();
		$.fancybox({'width':520, 'height':440, 'padding':20, 'type':'iframe', 'href':'/include/mailinglist.php?email='+str, 'showCloseButton':false});
		e.preventDefault();
	});
}

function fadeBanner( duration ) {
	fout = $(".banner div:visible");
	fin = fout.next();
	if ( fin.length == 0 )	fin = $(".banner div:first");
	fout.delay( duration ).fadeOut( duration );
	fin.delay( duration ).fadeIn( duration, function() { fadeBanner( duration ); });
}

function slideLocation() {
	$(".locations a").click( function(e) {
		e.preventDefault();
		$(this).next("div").slideToggle("slow");
	});
}

function storiesHover() {
	$(".storyimg").hover( function() {
		$("img", this).toggleClass("hidden");
	});
}


function formBg() {
	$("input:text, textarea", ".form_bg").focus( function() {
		$(this).addClass("focus");
	});
	
	$("input:text, textarea", ".form_bg").blur( function() {
		$(this).removeClass("focus");
	});
}

function slideToggle(el, bShow){
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: 600});
  } else {
    $el.animate({height: 0}, {duration: 600, complete:function (){
        $el.hide();
      }
    });
  }
}



function setCookie( name, value, expiredays ) {
	var exdate = new Date();
	exdate.setDate( exdate.getDate() + expiredays );
	document.cookie = name + "=" + escape( value ) + ( ( expiredays == null )? "" : ";expires=" + exdate.toGMTString() ) +  ";path=/";
}

function getCookie( name ) {
	if ( document.cookie.length > 0 ) {
		c_start = document.cookie.indexOf( name + "=" );
		if ( c_start != -1 ) {
			c_start = c_start + name.length + 1;
			c_end = document.cookie.indexOf( ";", c_start );
			if ( c_end == -1 )	c_end = document.cookie.length;
			return unescape( document.cookie.substring( c_start, c_end ) );
		}
	}
	return "";
}


