var scrolling = false;

//$(document).ready(function(){

	
function initPage(){
	// place the init functions in a separate function
	// that can be called by Infinite Scroll.
		
	$('#header-fader').not('.page-id-2 #header-fader').fadeOut(4000);
	
	// fader for the entry-header	
	$('.blog .entry-head,.single .entry-head,.pageid-2 .entry-head').hide();
	$('.entry-foot').hide();
	
	hfadeDone = true;
	navDone = true;
	
	$('#widgets-sidebar-1').prepend('<a id="home-link" href="/" >Grustler Home</a>');
	$('.widget_pages').show();
	//$(".parent-pageid-13 .widget_pages").fadeOut(6000);
	// $('.widget_pages li').not('.primary').fadeOut(4000);

	 $('#widgets-sidebar-1 a,.entry-head a,.entry-content a,.entry-content img, .arch-link, .content img').mouseover(function(e) {          
	        //Grab the title attribute's value and assign it to a variable  
	        var menutip = $(this).attr('title');      
	        
	        //Remove the title attribute's to avoid the native tooltip from the browser  
	        $(this).attr('title','');          
	          
	   		 }).mouseout(function() {  
	      
	        //Put back the title attribute's value  
	        $(this).attr('title', 'menutip');          
	 });
	

	$('#header').not('.pageid-2 #header').hover(		
		//Mouseover, fadeIn the hidden hover class	
		function() {
			$(this).children('div').stop(true, true).fadeIn(200);
		}, 
		//Mouseout, fadeOut the hover class
		function() {
			$(this).children('div').stop(true, true).fadeOut(2000);						
	});
	
	var hoverbg = {"background-position":"-200px 10px"};
	var normbg = {"background-position":"0px 10px"};
	
	$('#home-link').hover(
		function() {
			$(this).parent().css(hoverbg);
			return false;
		}, 
		//Mouseout, fadeOut the hover class
		function() {
			$(this).parent().css(normbg);
			return false;					
	});
	
	// set the onClick behavior of tweet popup links
	var x = $('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('type') == 'popup') {
			x[i].onclick = function () {
				return tweetpop(this.href)
			}
			x[i].title += '[popup]';
		}
	}
	
	
	$('.post').mouseover(function() {
	// post header options

		var picH = "-8px";
		if ($(this).hasClass('.category-media,.category-motto')) { picH = "-11px"; };
		
		var cssObj = {
			'top' : picH,
			'margin-top' : ''
		}
		
		$(this).find('.entry-head').css(cssObj);
				
		$(this).find('.entry-head').fadeIn(300);
		$(this).find('.entry-foot').fadeIn(300);
		return false;
	});
	
	$('.post').mouseleave(function() {
    	$(this).find('.entry-head').fadeOut('fast');
    	$(this).find('.entry-foot').fadeOut('fast');
    	return false;
	});
	
	//change nav /product/ to den of thieves
	/*
var prod = $('.page-item-13 a').attr('href');
	prod += "game-navigator/";
	$('.page-item-13 a').attr('href',prod);
*/
	
	// adds the "photog' class to all links in posts that are not <img>
	//$('.entry-content a').addClass('photog');
	//$('.entry-content a:has(img)').removeClass('photog');

	
	// mouseover for facebook button.
	$(".facebook a img,#submit,#buynow").hover(
		 function()
		 {
		  this.src = this.src.replace("-off","-on");
		 },
		 function()
		 {
		  this.src = this.src.replace("-on","-off");
		 }
	);
	
	// take images linked to attachment pages and link them to the post instead
	var postlink = $('a[rel*="bookmark"]').attr('href');
	var attsrc = $('a[rel*=attachment] img').attr('src');
	$('a[rel*=attachment]').attr('href', postlink);

	
	//slideshow fucntions
	var pointer = {'cursor':'pointer','color':'#2277DD'};
	var black = {'color':'#000000'};
	
	$('.next,.prev').hover(function() {
    	$(this).css(pointer);
    	return false;
      }, 
      function () {
        $(this).css(black);
      	return false;
	});

	var total = $('.scroller ul').children().size();
	var upnum = $('.currentpic').html();
	upnum = upnum * 1;
	$('.prev').fadeTo(10,.2);

	$('.next').click(		
		function() {
			if (scrolling == false) {
			
				if (upnum < total ) {
						upnum = upnum + 1;
						$('.currentpic').html(upnum);
						$('.prev').fadeTo(200,1);
						if (upnum == total) {
							$(this).fadeTo(200,.2);
						};
					} else {
						upnum = 1;
						$('.currentpic').html(upnum);
						$(this).fadeTo(200,1);
						$('.prev').fadeTo(200,.1);						
					}
			}
	});
	
	$('.prev').click(		
		function() {
			if (scrolling == false) {
				if (upnum > 1) {
						upnum = upnum - 1;
						$('.currentpic').html(upnum);
						$('.next').fadeTo(200,1);
						if (upnum == 1) {
							$(this).fadeTo(200,.2);
						};
					} else {
						upnum = 6;
						$('.currentpic').html(upnum);
						$(this).fadeTo(200,1);
						$('.next').fadeTo(200,.2);
					}
			}
	});


};

// Call the Init function.
$(document).ready(initPage);

function scrollInit(){
	// fader for the entry-header	
	$('.blog .entry-head,.single .entry-head,.pageid-2 .entry-head').hide();
	$('.entry-foot').hide();
	
	hfadeDone = true;
	navDone = true;


	 $('.entry-head a,.entry-content a,.entry-content img, .arch-link').mouseover(function(e) {          
	        //Grab the title attribute's value and assign it to a variable  
	        var menutip = $(this).attr('title');      
	        
	        //Remove the title attribute's to avoid the native tooltip from the browser  
	        $(this).attr('title','');          
	          
	   		 }).mouseout(function() {  
	      
	        //Put back the title attribute's value  
	        $(this).attr('title', 'menutip');          
	 });
	
	// set the onClick behavior of tweet popup links
	var x = $('a');
	for (var i=0;i<x.length;i++) {
		if (x[i].getAttribute('type') == 'popup') {
			x[i].onclick = function () {
				return tweetpop(this.href)
			}
			x[i].title += '[popup]';
		}
	}
	
	
	$('.post').mouseover(function() {
	// post header options

		var picH = "-8px";
		if ($(this).hasClass('.category-media,.category-motto')) { picH = "-11px"; };
		
		var cssObj = {
			'top' : picH,
			'margin-top' : ''
		}
		
		$(this).find('.entry-head').css(cssObj);
				
		$(this).find('.entry-head').fadeIn(300);
		$(this).find('.entry-foot').fadeIn(300);
		return false;
	});
	
	$('.post').mouseleave(function() {
    	$(this).find('.entry-head').fadeOut('fast');
    	$(this).find('.entry-foot').fadeOut('fast');
    	return false;
	});
	
	// mouseover for facebook button.
	$(".facebook a img,#submit,#buynow").hover(
		 function()
		 {
		  this.src = this.src.replace("-off","-on");
		 },
		 function()
		 {
		  this.src = this.src.replace("-on","-off");
		 }
	);
	
	// take images linked to attachment pages and link them to the post instead
	var postlink = $('a[rel*="bookmark"]').attr('href');
	var attsrc = $('a[rel*=attachment] img').attr('src');
	$('a[rel*=attachment]').attr('href', postlink);

	
};


$(function() {
	    $('.scroller').jCarouselLite({
	        btnNext: ".next",
	        btnPrev: ".prev",
	        speed: 600,
	        visible: 1,
	        circular: true,
	        beforeStart: function(a){startScroll()},
   			afterEnd: function(a) {endScroll()}
	    });
	    
	    // on post pages, takes attachment images, and removes link and title
	    // must happen after carousel script
	    var inner = $('a[rel*=attachment]');
		$(inner).attr('title','');
		var innerimg = $(inner).html();
		if (innerimg) {
		notitle = innerimg.replace(/title="[^"\r\n]*"/g,'title=""');
	 	$('.single a[rel*=attachment]').parent().html(notitle);
	 	}
});

function startScroll() {
	//alert('');
	scrolling = true;
}

function endScroll() {
	scrolling = false;
}
