//javascript
//anisfield
var total;
var current = 0;
var laston;
var slidetime = 5000;
var waittime = 5000;
var animating = false;
window.timer = null;
window.tosettime = null;
jQuery(document).ready(function($){
	if($('.eNewsletter').html()!=null){
		$('.eNewsletter form input').each(function(){
			if($(this).attr('id')!=undefined){
				switch($(this).attr('id')){
					case 'name':
					$(this).attr('rel','Name');
						if(!$(this).val() || $(this).val()==$(this).attr('rel')){
							$(this).val('Name').css({'color':'#CCC'});	
						}
					break;
					case 'fjltri-fjltri':
						$(this).attr('rel','Email');
						if(!$(this).val() || $(this).val()==$(this).attr('rel')){
							$(this).val('Email').css({'color':'#CCC'});	
						}
					break;
					default:
					
					break;
				}
			}
		});
		$('.eNewsletter form input').focus(function(){
			if($(this).val()!='Subscribe'){
				if($(this).val()==$(this).attr('rel')){
					$(this).val('').css({'color':'#4c4c4c'});
				}
			}
		}).blur(function(){
			if($(this).val()!='Subscribe'){
				if($(this).val()=='' || $(this).val()==' ' || $(this).val()==undefined || $(this).val()==null){
					$(this).val($(this).attr('rel')).css({'color':'#CCC'});
				}
			}
		});
		$('.eNewsletter form').slideUp(0);
		$('.eNewsletter').click(function(){
			$(this).css({'cursor':'default'}).find('form').slideDown(500);	
		});
	}
	
	setTimeout("jQuery('#main').css({'background-image':'url(/wp-content/themes/anisfieldwolf/images/page-corner.jpg)'});",1500);
	if($('.book-menu').html()!=null){
		if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		}else{
		$(window).scroll(function(){
			offset = $(window).scrollTop();
			if(offset>150/*192*/){
				$('.book-menu').addClass('fixedbar');
				//$('.book-menu').stop().animate({'padding-top':(offset-150)+'px'},500);
			}else{
				//$('.book-menu').stop().animate({'padding-top':(0)+'px'},500);
				$('.book-menu').removeClass('fixedbar');
			}
			//console.log(offset);
		});
		if($(window).scrollTop()>150/*192*/){
			$('.book-menu').addClass('fixedbar');
			//$('.book-menu').stop().animate({'padding-top':(offset-150)+'px'},100);
		}else{
			//$('.book-menu').stop().animate({'padding-top':(0)+'px'},100);
			$('.book-menu').removeClass('fixedbar');
		}
		}
	}
	if($('.home').html()!=null){
		total = images.length-1;
		for(var i=0;i<images.length-1;i++){
			if(i>6){
				$('#book-'+i+' .book-cover-holder').html(images[i]);	
			}
		}				
		$('#home-feature .feature-book').hover(function(){
			//on
			clearInterval(window.tosettime);
			stopTimer();
			$(this).find('.overlay').stop(true,false).animate({'top':(290-$(this).find('.overlay').height()-10)+'px'},500);
		},function(){
			//off	
			window.tosettime = setInterval("setTimer()",waittime);
			$(this).find('.overlay').stop(true,false).animate({'top':'290px'},500);
		}).click(function(){
			window.location.href=$(this).find('.more-link').attr('href');	
		});
		$('#feature-navbar .prev-arrow').click(function(){
			if(animating==false){
			clearInterval(window.tosettime);
			stopTimer();
			slideBackFrom(current);
			}
			return false;	
		}).fadeOut();
		$('#feature-navbar .next-arrow').click(function(){
			if(animating==false){
			clearInterval(window.tosettime);
			stopTimer();
			slideTo(laston+1);
			}
			return false;	
		});
		var books = $('.feature-book');
		var tempwidth = -20;
		for(var i = current;i<books.length;i++){
			tempwidth += $(books[i]).width()+20;
			if(tempwidth<=980){
				laston = i;	
			}else{
				break;	
			}
		}
		$('.feature-list').animate({'left':'0px'},500,function(){
			window.tosettime = setInterval("setTimer()",waittime);
		});
	}
	
	if($('#contribute-form').html()!=null){
		if(!$('#contribute-form').hasClass('open')){
			$('#contribute-form form').slideUp(0);
			$('#contribute-form').css({'width':'24%','padding-top':($('#contribute-form').css('padding-top'))+'px','padding-bottom':($('#contribute-form').css('padding-bottom'))+'px'});
		}
		$('#contribute-link').click(function(){
			if($('#contribute-form').hasClass('open')){
				
			}else{
				$('#contribute-form>strong').first().css({'cursor':'default'});
				$('#contribute-form').addClass('open').animate({'width':'96%','padding-top':'8px','padding-bottom':'8px'},200,function(){$('#contribute-form form').slideDown(500);});
				return false;	
			}
		});
		$('#contribute-form>strong').first().css({'cursor':'pointer'}).click(function(){
			if(!$('#contribute-form').hasClass('open')){
				$('#contribute-form>strong').first().css({'cursor':'default'});
				$('#contribute-form').addClass('open').animate({'width':'96%','padding-top':'8px','padding-bottom':'8px'},200,function(){$('#contribute-form form').slideDown(500);});
			}
		});
	}
	
	if($('.news-listing').html()!=null){
		//news page
		$('.news-listing a.audio').each(function(){
			$(this).parent().addClass('closed');
			$(this).parent().find('.audio-player').delay(1).slideUp(0);
			$(this).click(function(){
				if($(this).parent().hasClass('closed')){
					$(this).parent().removeClass('closed').find('.audio-player').slideDown(500);	
				}
				return false;
			});	
		});
	}
	
	$('a').each(function(index){
		var ah = '';
		if($(this).attr('href')!=undefined){
			ah = $(this).attr('href');
			ah = ah.toLowerCase();
			if(ah.substr(0,4)=='http'){
				var domain = ah.replace('https://','');
				domain = domain.replace('http://','');
				domain = domain.split('.');
				if(domain[0]=='www'){
					domain = domain[1];
				}else{
					domain = domain[0];	
				}
				if(domain!='anisfield-wolf'){
					$(this).addClass('external');
					$(this).attr('onclick','javascript:ShowWindow(this.href); return false;');	
				}
			}
		}
	});
});
function slideTo(which){
	//console.log(jQuery('#book-'+which).position());
	if(jQuery('#book-'+which).html()!=null){
	animating = true;
	var p = jQuery('#book-'+which).position();
	jQuery('.feature-list').animate({'left':'-'+(p.left)+'px'},500,function(){
		current = which;
		if(which>0){
			jQuery('.prev-arrow').fadeIn(200);	
		}else{
			jQuery('.prev-arrow').fadeOut(200);	
		}
		var books = jQuery('.feature-book');
		var tempwidth = -20;
		for(var i = current;i<books.length;i++){
			tempwidth += jQuery(books[i]).width()+20;
			if(tempwidth<=980){
				laston = i;	
			}else{
				break;
			}
		}		
		//console.log(laston,books.length);
		if(laston<(books.length-1)){
			jQuery('.next-arrow').fadeIn(200);	
		}else{
			jQuery('.next-arrow').fadeOut(200);	
		}
		animating = false;
		window.tosettime = setInterval("setTimer()",waittime);
	});
	}
}
function slideBackFrom(which){
	which = which-1;
	if(jQuery('#book-'+which).html()!=null){
		animating = true;
		var books = jQuery('.feature-book');
		var tempwidth = -20;
		var first
		for(var i = which;i>-1;i--){
			tempwidth += jQuery(books[i]).width()+20;
			if(tempwidth<=980){
				first = i;	
			}else{
				break;
			}
		}
		var p = jQuery('#book-'+first).position();
		jQuery('.feature-list').animate({'left':'-'+(p.left)+'px'},500,function(){
			current = first;
			if(first>0){
				jQuery('.prev-arrow').fadeIn(200);	
			}else{
				jQuery('.prev-arrow').fadeOut(200);	
			}
			var books = jQuery('.feature-book');
			var tempwidth = -20;
			for(var i = current;i<books.length;i++){
				tempwidth += jQuery(books[i]).width()+20;
				if(tempwidth<=980){
					laston = i;	
				}else{
					break;
				}
			}		
			//console.log(laston,books.length);
			if(laston<(books.length-1)){
				jQuery('.next-arrow').fadeIn(200);	
			}else{
				jQuery('.next-arrow').fadeOut(200);	
			}
			animating = false;
			window.tosettime = setInterval("setTimer()",waittime);
		});
	}
}
function setTimer(){
	clearInterval(window.timer);
	clearInterval(window.tosettime);
	//console.log('set the timer');
	window.timer = window.setInterval("timerHandle()",slidetime);
}
function stopTimer(){
	clearInterval(window.timer);
	clearInterval(window.tosettime);
	//console.log('stop the timer');
}
function timerHandle(){
	stopTimer();
	//console.log('timerHandle',laston,total);
	if(laston>=(total-1)){
		slideTo(0);
	}else{
		slideTo(laston+1);
	}
}

function ShowWindow(url,width,height) {
	_gaq.push(['_trackPageview','/external/'+url]);
	var wh = 700;
	var ww = 1000;
	if(width!=null){
		ww = width;
	}
	if(height!=null){
		wh = height;
	}
    objMovieWindow = window.open(url, "NSideasWindow", "width="+ww+", height="+wh+", top=0, left=0, status=1, toolbar=1, menubar=1, location=1, directories=1, scrollbars=1, resizable=1");
    objMovieWindow.focus();
} 

/*function setWatched(obj){
	if(obj){obj.className = 'viewedMovie';}
	return false;
}*/
