var isIphone = false;

$(document).ready(function()
{
	logger("HelloWorld");

	var isHomePage = $.find(".videoHolder");

	if(isHomePage != 0)
	{
		homeFader();
		homeFader2();
	}

	clearTextbox();
	searchBarTricks();

	cleanArchives();

	$("p:has(img)").css('margin' , '0');

	if($('body').hasClass('blog'))
	{
		$("#content").prepend('<h1 class="entry-title">News</h1>');
	}

	var userAgent = navigator.userAgent;

	if(userAgent.indexOf("iPhone") > 0)
	{
		isIphone = true;
	}

	findCatPages();

	$("#searchsubmit").addClass('button');

});

/*
	clear the subForm boxes on focus

	if there's nothing entered, return them to the
	original state.
*/
function clearTextbox()
{
	var tempNameVal = $("#name").val();
	var tempEmailVal = $("#vdrhhk-vdrhhk").val();

	$("#name").focus(function()
	{
		var thisEle = $(this);
		thisEle.attr('value', '');
	});

	$("#name").blur(function()
	{
		var thisEle = $(this);
		if(thisEle.val() == "")
		{
			thisEle.attr('value', tempNameVal);
		}
	});

	$("#vdrhhk-vdrhhk").focus(function()
	{
		var thisEle = $(this);
		thisEle.attr('value', '');
	});

	$("#vdrhhk-vdrhhk").blur(function()
	{
		var thisEle = $(this);
		if(thisEle.val() == "")
		{
			thisEle.attr('value', tempEmailVal);
		}
	});
}

/*
	hide and show elements of the search bar on and off focus
*/
function searchBarTricks()
{
	$("#s").val('Search');
	$("#s").focus(function()
	{
		$("#s").val('');
		$("#s").css('padding-left','7px');
		$(".navBarMagGlass").css('display','none');
		$(".navBarSearch #searchsubmit").css('display','inline');
		$(".navBarSearch #searchsubmit").css('left', '127px');
		$("#s").css('padding-right', '16px');
	
		if(isIphone)
		{
			$(".navBarSearch #searchsubmit").css('left', '134px');
			$(".navBarSearch #searchsubmit").css('top', '3px');
		}
	
	});

	$("#s").blur(function()
	{
		var thisEle = $(this);
		if(thisEle.val() == "")
		{
			thisEle.attr('value', "Search");
			$("#s").css('padding-left','20px');
			$("#s").css('padding-right', '0px');
			$(".navBarMagGlass").css('display','inline');
			$(".navBarSearch #searchsubmit").css('display','none');
		}
	});
}

/*
	remove anything set to 1977 in the archive widget.
*/
function cleanArchives()
{
	var widgetLiA = $(".widget_archive").find('li a');
	var widgetLi = $(".widget_archive").find('li');

	for(var i = 0; i < widgetLiA.length; i++)
	{
		var tempString = $(widgetLiA[i]).html().toString();
		if(tempString.indexOf("1977")>0)
		{
			$(widgetLi[i]).remove();
		}
	}
}

function findCatPages()
{
	var pathName = window.location.pathname;

	if(pathName.indexOf("category")>0)
	{
		var postArray = $("#content").find(".postHolder");

		var firstPost = $(postArray[0]).find(".post");

		//logger(firstPost.html());

		firstPost.addClass("catPostPush");
	}
}

function homeFader()
{
	//Image fader
		
	// fading vars -------------------------------------------------
    var ImageArray = []; // array to hold the src of the backgroun img	
    var ImageArragEnd = ImageArray.length;	
    var BannerLable = 0; // use to lable everything	
    var counter = 0; // counts were the banner is upto
    var BGcounter = 0; // counts were the banner background src is upto
    var timer; // timer val

    // -- start functions ------------------------------------------

    //fading labler
    $(".videoHolder").children("img").each(function(){ // lables everything and add to array so can be use for fading.
           //var BannerSRC = $(this).children().attr("src");
           BannerLable ++; // just a counter for the class adder
           $(this).addClass("f01image"+ BannerLable +""); // adds class like "image1", "image2"
           $(this).css('display','none');
           ImageArray.push(BannerLable) // push then
           ImageArragEnd = ImageArray.length; // updating the array
    });

    // test to see if there is 1 or more images ---------------------
    if(ImageArragEnd == 1){
           //do nothing!
           $(".f01image1").css("display", "inline");
           }else{
           fadingFun(); // start the banner fade
           //$("#fader").children().fadeOut(0); // hides all of the slides
           $(".f01image1").fadeIn(0); // hides all of the slidess
    }
    //make the squares for the current image indication
    /*
    if(ImageArragEnd != 1)
    {
	    for(var i = 1; i<ImageArragEnd; i++)
	    {

	    	$('<div id="indicator' + i + '" class="imageIndicator"></div>').appendTo("#rotatorIndicatorHolder");
	    	$("#indicator1").css('background-color', '#F2C300');
	    	//the first one wont take the colour in the fader loop... so set it here.
	    }
	}
	*/

    // fading FUNction -----------------------------------------------
    function fadingFun()
    {

	    ImageArragEnd = (ImageArray.length) + 1; //UPDAT PUT BOTH INTO A FUCNTION as a call back of each....
	    counter ++;
	    //BGcounter ++;
	    
	           // $("#fader").css("background-image","url("+ ImageArray[BGcounter] +")");      // get this working
		if(counter == ImageArragEnd ){  // check were its upto in the line and if its the last one then *restart*
	           counter = 1;
	    }
	    
	    //$(".sectionHeading").text(counter); //use this for testing the counter...
	    // $(".imageIndicator").css('background-color','#A38816');
	    // $("#indicator" + counter).css('background-color','#F2C300');

	    $(".faderItem").fadeOut(1000),
	         clearTimeout(timer);
	       timer = setTimeout(eval("fadingFun"),"5000");  // rrestart the functions
	        $(".f01image"+ counter +"").fadeIn(1000); //.. shows box one the hides it so i can be faded in! *failsafe

             // restarter
    }
}

function homeFader2()
{
	//Image fader
		
	// fading vars -------------------------------------------------
    var ImageArray = []; // array to hold the src of the backgroun img	
    var ImageArragEnd = ImageArray.length;	
    var BannerLable = 0; // use to lable everything	
    var counter = 0; // counts were the banner is upto
    var BGcounter = 0; // counts were the banner background src is upto
    var timer; // timer val

    // -- start functions ------------------------------------------

    //fading labler
    $(".videoHolder2").children("img").each(function(){ // lables everything and add to array so can be use for fading.
           //var BannerSRC = $(this).children().attr("src");
           BannerLable ++; // just a counter for the class adder
           $(this).addClass("f02image"+ BannerLable +""); // adds class like "image1", "image2"
           $(this).css('display','none');
           ImageArray.push(BannerLable) // push then
           ImageArragEnd = ImageArray.length; // updating the array
    });

    // test to see if there is 1 or more images ---------------------
    if(ImageArragEnd == 1){
           //do nothing!
           $(".f02image1").css("display", "inline");
           }else{
           fadingFun2(); // start the banner fade
           //$("#fader").children().fadeOut(0); // hides all of the slides
           $(".f02image1").fadeIn(0); // hides all of the slidess
    }
    //make the squares for the current image indication
    /*
    if(ImageArragEnd != 1)
    {
	    for(var i = 1; i<ImageArragEnd; i++)
	    {

	    	$('<div id="indicator' + i + '" class="imageIndicator"></div>').appendTo("#rotatorIndicatorHolder");
	    	$("#indicator1").css('background-color', '#F2C300');
	    	//the first one wont take the colour in the fader loop... so set it here.
	    }
	}
	*/

    // fading FUNction -----------------------------------------------
    function fadingFun2()
    {

	    ImageArragEnd = (ImageArray.length) + 1; //UPDAT PUT BOTH INTO A FUCNTION as a call back of each....
	    counter ++;
	    //BGcounter ++;
	    
	           // $("#fader").css("background-image","url("+ ImageArray[BGcounter] +")");      // get this working
		if(counter == ImageArragEnd ){  // check were its upto in the line and if its the last one then *restart*
	           counter = 1;
	    }
	    
	    //$(".sectionHeading").text(counter); //use this for testing the counter...
	    // $(".imageIndicator").css('background-color','#A38816');
	    // $("#indicator" + counter).css('background-color','#F2C300');

	    $(".faderItem2").fadeOut(1000),
	         clearTimeout(timer);
	       timer = setTimeout(eval("fadingFun2"),"5000");  // rrestart the functions
	        $(".f02image"+ counter + "").fadeIn(1000); //.. shows box one the hides it so i can be faded in! *failsafe

             // restarter
    }
}



function logger(msg)
{
	try
	{
		console.log(msg);
	}
	catch(e)
	{}
}
