/*
	ADDS MOBILE CSS
*/


// adds iPhone css for iPhones and BlackBerrys
if( (navigator.userAgent.indexOf('iPhone') != -1) ||
 (navigator.userAgent.indexOf('iPod') != -1) || 
 ( navigator.userAgent.indexOf('BlackBerry') != -1 ) ) {


	// iPhone layout
	var cssNode = document.createElement('link');
	cssNode.setAttribute('rel', 'stylesheet');
	cssNode.setAttribute('type', 'text/css');
	cssNode.setAttribute('href', 'http://www.rowast.com/testinggrounds/wp-content/themes/newish/new page/iPhone.css');
	cssNode.setAttribute('media', 'all');
	document.getElementsByTagName('head')[0].appendChild(cssNode);

} // end if



/*
	ADDS MOBILE CSS
*/




jQuery().ready(function($){


/*
	PARTIALLY OPAQUE BG'S W/O IMG
*/

	// partially opaque backgrounds without img's
	$('.header').css({opacity : 0.6});
	$('#footerx').css({opacity : 0.3});

/*
	PARTIALLY OPAQUE BG'S W/O IMG
*/




/*
	HEADER MENU
*/


	// makes sure all header menus are closed
	function closeMenu() {
			$('.contactheader').animate({opacity : 0, width: 'hide', height: 'hide'}, 500);
			$('.header a[href *= "contact"]').removeClass('currentmenu');
			
			$('.socialheader').animate({opacity : 0, width: 'hide', height: 'hide'}, 500);
			$('.header a[href *= "social"]').removeClass('currentmenu');
	} // end function


	// toggle social menu in header, open and close
	$('.header a[href *= "social"]').toggle(
		function() {
			closeMenu();
			$('.socialheader').animate({opacity : 1.0, width: 'show', height: 'show'}, 500);
			$('.header a[href *= "social"]').addClass('currentmenu');
			return false;

		}, // end function
		function() {
			$('.socialheader').animate({opacity : 0, width: 'hide', height: 'hide'}, 500);
			$('.header a[href *= "social"]').removeClass('currentmenu');
			return false;

		} // end function
	); // end toggle



	// toggle contact menu in header, open and close
	$('.header a[href *= "contact"]').toggle(
		function() {
			closeMenu();
			$('.contactheader').animate({opacity : 1.0, /*width: 'show',*/ height: 'show'}, 500);
			$('.header a[href *= "contact"]').addClass('currentmenu');
			return false;

		}, // end function
		function() {
			$('.contactheader').animate({opacity : 0, /*width: 'hide',*/ height: 'hide'}, 500);
			$('.header a[href *= "contact"]').removeClass('currentmenu');
			return false;

		} // end function
	); // end toggle











	// checks if email address as @ symbol and dot in correct position
	function email_chk() {
		
		val = $('#contact_email').val();
		apos = val.indexOf('@');
		dotpos = val.lastIndexOf('.');
			
		if ( apos<1 || dotpos-apos<2 ) {
			$('#contact_email').addClass('incomplete');
		} // end if
		else {
			$('#contact_email').removeClass('incomplete');
		} // end else
	
	} // end function



	// checks that subject line is filled in
	function subject_chk() {

		sub = $('#contact_subject').val()
		if( sub == '' || sub == null ) {
			$('#contact_subject').addClass('incomplete');
		} // end if
		else {
			$('#contact_subject').removeClass('incomplete');
		} // end else

	} // end function


	// checks that message is filled in
	function msg_chk() {
	
		msg = $('#contact_message').val()
		
		if( msg == '' ) {
			$('#contact_message').addClass('incomplete');
		} // end if
		else {
			$('#contact_message').removeClass('incomplete');
		} // end else

	} // end function






	$('#contact_email').keyup(
		function() {
			email_chk();
		}
	); // end keyup


	$('#contact_email').keydown(
		function() {
			email_chk();
		}
	); // end keydown


	$('#contact_email').focus(
		function() {
			email_chk();
		}
	); // end focus


	// if email address is filled in fade out tool tip
	$('#contact_email').blur(
		function() {
			email_chk();

			if( $(this).val() != '' ) 
				$(this).next().fadeOut(500);
		}
	); // end blur


	$('#contact_subject').keyup(
		function() {
			subject_chk();
		}
	); // end keyup


	$('#contact_subject').keydown(
		function() {
			subject_chk();
		}
	); // end keydown


	$('#contact_subject').focus(
		function() {
			subject_chk();
		}
	); // end focus


	// if subject is filled in fade out tool tip
	$('#contact_subject').blur(
		function() {
			subject_chk();

			if( $(this).val() != '' ) 
				$(this).next().fadeOut(500);
		}
	); // end blur


	$('#contact_message').keyup(
		function() {
			msg_chk();
		}
	); // end keyup


	$('#contact_message').keydown(
		function() {
			msg_chk();
		}
	); // end keydown


	$('#contact_message').focus(
		function() {
			msg_chk();
		}
	); // end focus


	// if message is filled in fade out tool tip
	$('#contact_message').blur(
		function() {
			msg_chk();
			
			if( $(this).val() != '' ) 
				$(this).next().fadeOut(500);
		}
	); // end blur


	$('.contactheader input').focus(
		function() {
			$(this).next().fadeIn(500);
		}
	); // end focus


	$('.contactheader textarea').focus(
		function() {
			$(this).next().fadeIn(500);
		}
	); // end focus



	$('#minicontact').submit(
		function() {
			
			// boolean variable that tracks whether form submission is possible
			chk = false;


			// run checks on all form elements
			subject_chk();
			msg_chk();
			email_chk();





			// check all form elements to see if incomplete class is attached and if so...
			// flag it and set chk to true to stop form subnission
			$('.contacttip').each(
				function(i) {

					if( $(this).prev().attr('class') == 'incomplete' ) {
						$(this).find('strong').html('this is required, sorry');
						$(this).fadeIn(500);
						
						chk = true;
					} // end if
						
				} // end function
			); // end each



			// if form submission is blocked then flags are already up
			if( chk == false ) {

				// put form elements into variables
				addy = $('#contact_email').val();
				sub = $('#contact_subject').val();
				msg = $('#contact_message').val();




				$.post(
					'http://rowast.com/testinggrounds/wp-content/themes/newish/contact.php', {
						// names of form elements & info being passed
						email: addy, 
						subject: sub, 
						message: msg
					},
					// function to fire once post is successful (only fires if successful)
					// response is echoed from php
					function(response) {
						if(response == 'Mail Sent') {
							$('.contactheader').append('<span id="x">xyz</span>');
							$('#x').animate({height: 'show'}, 1000);
//							alert('UH OH: ' + response);
						}
						else {
							alert(response);
						}
					} // end function
				); // end function


			
			} // end else

			// stop form submission from refreshing the page
			return false;

		} // end function
	); // end submit





	










/*
	HEADER MENU
*/






/*
	MAKES SIDEBAR AND CONTENT COLUMNS EQUAL WITHOUT BACKGROUND IMG'S
*/

	// height of sidebar and content
	sidebar_height = $('#sidebar').height();
	content_height = $('#content').height();


	// if sidebar is shorter than content make it equal
	if( sidebar_height < content_height ) {	
		$('#sidebar').css({ 'min-height' : content_height });
	} // end if
	else {
		$('#content').css({ 'min-height' : sidebar_height });
	} // end else
	

/*
	MAKES SIDEBAR AND CONTENT COLUMNS EQUAL WITHOUT BACKGROUND IMG'S
*/



/*
	SEARCH RELATED FUNCTIONS
*/


	// when search is focused on make blank if form is default
	$('#txtSearch').focus(function() {
		if($('#txtSearch').val() == 'search' || $('#txtSearch').val() == '') {
			$('#txtSearch').val(''); //= "";
		}
	}); // end function
	

	// when search is left if its empty return it to saying search
	$('#txtSearch').blur(function() {
		if($('#txtSearch').val() == '') {
			$('#txtSearch').val('search');
		}
	}); // end function


/*
	SEARCH RELATED FUNCTIONS
*/



/*
	BLOGROLL FAVICONS
*/


	// if #readerpublishermodule1 exists
	if($('#readerpublishermodule1').length > 0) {

		// gather links in blogroll
		var blogrollLink = document.getElementById('readerpublishermodule1').getElementsByTagName("a");


		// for every blogroll link provide a favicon img
		for(i=0; i < blogrollLink.length; i++) {

			// get link url
			favicon = blogrollLink[i].href;
			// slice link url into root
			favicon = favicon.substring(7, favicon.indexOf('/', 7));
			// put link url into google favicon retriever
			favicon = 'http://www.google.com/s2/favicons?domain=' + favicon;

			// insert img into link
			blogrollLink[i].innerHTML = '<img src="' + favicon + '" alt="blogroll favicon"/>' + blogrollLink[i].innerHTML;
			// force link to open in new window
			blogrollLink[i].target = '_blank';

		} // end for

	} // end if
	
/*
	BLOGROLL FAVICONS
*/




/*
	FUNCTION TO SHOW BANNERS IN SIDEBAR
*/


	// toggles banners hiding/showing
	$("#bannersLink").toggle(
		// hide banners
		function(){
			$("#bannersDiv").animate({ width: 'hide', height: 'hide', opacity: 'hide' }, 500);
			$$("bannersLink").innerHTML = '[+]';
		}, // end hife banner div
		function(){
			$("#bannersDiv").animate({ width: 'show', height: 'show', opacity: 'show' }, 500);
		     $$("bannersLink").innerHTML = '[ - ]';
   		} // end show banner div
   ); // end toggle


	// hide banners by default
	$("#bannersLink").trigger('click');



/*
	FUNCTION TO SHOW BANNERS IN SIDEBAR
*/




/*
	FUNCTION TO MAKE TIPS SHOW UP IN SIDEBAR
*/




	$('#sidebar div').hover(
		function() {
			$('#sidebar div:hover .side_tip').fadeIn(750);
		},
		function() {
			$('#sidebar div:hover .side_tip').fadeOut(750);
		}
	); // end hover




/*
	FUNCTION TO MAKE TIPS SHOW UP IN SIDEBAR
*/





/*
	FUNCTION TO MAKE SELECTED LINKS OPEN IN NEW WINDOW
*/

	// get all links from footer
	a = document.getElementById('credits_footer').getElementsByTagName('a');


	// cycle through each link
	for(i=0; i < a.length; i++) {

		// make valid xhtml open in new window
		if(a[i].innerHTML == 'valid xhtml'){
			a[i].target = '_blank';
		} // end if

		// make valid css open in new window
		if(a[i].innerHTML == 'valid css*'){
			a[i].target = '_blank';
		} // end if

	} // end for


/*
	FUNCTION TO MAKE SELECTED LINKS OPEN IN NEW WINDOW
*/

































}); // end function
