function moveScroller() {
	var a = function() {
	  	var b = $(window).scrollTop();
	  	var d = 0;
	  	var c=$("#scroller");
	  	if (b>d) {
	    	c.css({position:"fixed",top:"0px"})
	  	} else {
	    	if (b<=d) {
	      		//c.css({position:"absolute",top:""})
	    	}
	  	}
	};
	$(window).scroll(a);a()
}

try {
  	Typekit.load({
    	active: function() {
	      	function fadeInTypekit() {
	        	$('.meta').hide().css('visibility', 'visible').fadeIn("fast");
	      	}
	      	if ($.browser.msie && parseInt($.browser.version) <= 8) {
	        	$('.meta').css('visibility', 'visible');
	      	} else {
	        	setTimeout(function() { fadeInTypekit(); }, 50);
	      	}
    	}
  	})
} catch(e) {
}
		  
function calcFrameHeight(){
	$("#test").height($("#test").contents().find(".wrapper").height()+50);
}

function setEvents(){
}

function openItem(id){
	$("#item"+id).trigger("click");
	preloadedId = id;
	return false;
}

var preloadedId = "";

$(function() {
	if(!($.browser.msie && $.browser.version=="6.0")){
		moveScroller();
	}
	
	
	
	$("a.login").fancybox({
		'type' : 'iframe'
	});

	
	var options = { 
        beforeSerialize:  showRequest
    }; 
	
	$('#searchform').ajaxForm(options);
	
	$("#test").each(function(){
		if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
			$(this).trigger("load");
	}); 
	
	if(document.location.href.toString().indexOf("thanks") > -1){
		/*$("#test").attr("src","/pages/thanks.php");*/
	}else if(document.location.href.toString().indexOf("shop") > -1){
		$("#test").attr("src","/pages/shop.php");
	}else{
		$("#test").attr("src","/pages/home.php");
	}
	
	$(".rightlink").click(function(el){
		$("#test").attr("src",$(this).attr("href"));
		$(".rightlink").each(function(){
			$(this).parent().removeClass("active");
		});
		$(this).parent().addClass("active");
		return false;
	});
	
	$("#left").width($("body").width()-420);
	if($("#left").width() < 600){
		$("#left").width(600);
	}
	
	
	
	$(".item_content:not(.first,.last) .btnBox .prev").live("click",function(){
		$(this).closest(".item").prev(".eachpost").trigger("click");
		return false;
	});
	$(".item_content:not(.first,.last) .btnBox .next").live("click",function(){
		$(this).closest(".item").next(".eachpost").trigger("click");
		return false;
	});
	$(".item_content .btnBox .close").live("click",function(){
		$(this).closest(".item").removeClass("threecols");
		arrange();
		
		document.title = "O.K. PERIODICALS / a biannual magazine produced by creative professionals for creative professionals";
		return false;
	});
	
	
	
	
	
	$(".related-items a").live("click",function(){
		//openItem($(this).attr("href"));
		document.location = $(this).attr("href");
		return false;
	});
	
	
	var options = { 
			        success:  formSubmitted
			    }; 
	
	var options2 = { 
			        success:  formSubmitted2
			    };
				
				 $('#subscribe').ajaxForm(options);
				 $('#subscribe2').ajaxForm(options2);
	
	
	if(preloadedId != ""){
		//document.title = "PRELOADED"+preloadedId;
		//openItem(preloadedId);
	}
	
	if($(".eachpost").length > 0){
		$("#left").height($(".eachpost").last().position().top + 60);
	}
	
	$(".footer-inner").width($("body").width()-40);
	$(".pagination-holder").width($("#left").width());
	
	$("#cats_li").mouseenter(function(){
		$(this).toggleClass("over");
	}).mouseleave(function(){
		$(this).toggleClass("over");
	});
});

function formSubmitted(responseText){
		if(responseText.indexOf("has been added") > -1){
			$("#submitbtn").hide();
			$("#returnmsg").removeClass("wrong");
			$("#returnmsg").addClass("good");
		}else{
			$("#returnmsg").addClass("wrong");
			$("#returnmsg").removeClass("good");
		}
		$("#returnmsg").html(responseText);
}

function formSubmitted2(responseText){
	alert(responseText);
		if(responseText.indexOf("has been added") > -1){
			$("#submitbtn2").hide();
			$("#topsubform").hide();
			$("#topsub").html("E-mail address added!");
		}else{
			$("#topsubform").addClass("wrong");
			$("#topsubform").removeClass("good");
		}
}

$(window).resize(function(){
	$("#left").width($("body").width()-420);
	if($("#left").width() < 600){
		$("#left").width(600);
	}
	$(".footer-inner").width($("body").width()-40);
	if($(".eachpost").length > 0){
		$("#left").height($(".eachpost").last().position().top + 60);
	}
	$(".pagination-holder").width($("#left").width());
});

function setPage(num){
	//document.title = document.location + document.location.toString().indexOf("search");
	if(document.location.toString().indexOf("recent") > -1 || document.location.toString().indexOf("thanks") > -1){
		document.location = "/recent/P"+num+"/";
	}
	if(document.location.toString().indexOf("popular") > -1){
		document.location = "/popular/P"+num+"/";
	}
	if(document.location.toString().indexOf("search") > -1){
		TStr = document.location.toString().split("http://").join("").split("www.").join("");
		TArr = TStr.toString().split("/");
		//document.title = TArr.toString();
		if(TArr.length > 2){
			document.location = "/search/"+TArr[2]+"/P"+num+"/";
		}else{
			document.locaiton = "/search/P"+num+"/";
		}
	}
}

function showRequest(){
	// ssubmitSearch();
	//document.title = "va'";
	document.location = "/search/"+$("#search").val().toString().toLowerCase()+"/";
	return false;
}

function submitSearch(){
	document.location = "/search/"+$("#search").attr("value")+"/";
	return false;
}


$("#left .eachpost, .related-items a").live("mousemove",function(e){
	if(!$(this).hasClass("threecols")){
		$(".tooltip .by").html("Posted by "+$(".by",this).val());
		$(".tooltip .date").html($(".dater",this).val());
		$(".tooltip h1").html($(".titler",this).val());
		$(".tooltip").show();
		$(".tooltip").css({
            top: (e.pageY) + "px",
            left: (e.pageX +8) + "px"
        });

	}
});


$("#left .eachpost").live("mouseleave",function(){
	$(".tooltip").hide();
});

$("#left .eachpost").live("click",function(){
		if(!($(this).hasClass("threecols"))){
			//console.log("GO");
			
	
	
			$(".tooltip").hide();
			$.get("/actions/item-clicks.php?id="+$(this).attr("id").split("item").join(""));
			
			TStr = document.location.toString().split("http://").join("").split("www.").join("");
			TArr = TStr.toString().split("/");
			if(document.location.toString().indexOf("#") > -1 ){
			window.location.hash = "/"+$(".urlstr",this).val()+"/";
			}
			else if((TArr[1] == "search" && TArr.length < 5)){
				window.location.hash = "/"+$(".urlstr",this).val()+"/";
			}else if((TArr[1] != "search" && TArr.length < 4) || (TArr[2].length < 5 && TArr.length < 5)){
				window.location.hash = "/"+$(".urlstr",this).val()+"/";
			}
			var thisEl = $(this);
			if(typeof type != "undefined"){
				setCategory(type);
			}
			serialImages = $(".serialImages",this).attr("value");
				
			aImagesToPreload = serialImages.split(",");
			saveSrc = $(".item_intro .thumb_item",this).attr("src");
			$(".item_intro .thumb_item",this).attr("src","/assets/img/loading.gif");
			 
			image_url = aImagesToPreload;
			imagesloaded = Array();
			
			var browser=navigator.appName;
		   	var i = 0;
		   	var counter = 0;
		    //console.log(aImagesToPreload);
		    if(aImagesToPreload.length == 1 && aImagesToPreload[0] == ""){
		    	if(browser=="Microsoft Internet Explorer"){
					loopDelay(200);
				};
				
				$(".item_intro .thumb_item",thisEl).attr("src",saveSrc);
				//document.title = thisEl.attr("id") + " - " + saveSrc;
				
				$(".eachpost").removeClass("threecols");
				thisEl.addClass("threecols");
				window._gaq.push(['_trackPageview', $(".urller",thisEl).val()]);
				
				window._gaq.push(['_trackEvent', 'Items', 'Open', $(".urller",thisEl).val()]);
				var images = $(".loadImages",thisEl).attr("value").split("width=140").join("width=440");
				$(".imageArea",thisEl).html(images);
				document.title = "O.K. PERIODICALS / "+$(".titler",thisEl).val();							
				arrange();
		    }
		    
		   	for(i=0; i<aImagesToPreload.length; i++){
		   		preload_image_object = new Image();
		   		preload_image_object.onload = function(){
				//document.title += "IMAGE-"+counter;
					counter++;
					imagesloaded[i] = "1";
					if(counter == aImagesToPreload.length){
						if(browser=="Microsoft Internet Explorer"){
							loopDelay(200);
						};
						
						$(".item_intro .thumb_item",thisEl).attr("src",saveSrc);
						//document.title = thisEl.attr("id") + " - " + saveSrc;
						
						$(".eachpost").removeClass("threecols");
						thisEl.addClass("threecols");
						window._gaq.push(['_trackPageview', $(".urller",thisEl).val()]);
						
						window._gaq.push(['_trackEvent', 'Items', 'Open', $(".urller",thisEl).val()]);
						var images = $(".loadImages",thisEl).attr("value").split("width=140").join("width=440");
						$(".imageArea",thisEl).html(images);
						document.title = "O.K. PERIODICALS / "+$(".titler",thisEl).val();							
						arrange();
					
					}	
				};	
				if(image_url[i].toString().indexOf("youtube") > -1){
					document.title = "YT";
					counter++;
					if(counter == aImagesToPreload.length){
						if(browser=="Microsoft Internet Explorer"){
							loopDelay(200);
						};
						
						$(".item_intro .thumb_item",thisEl).attr("src",saveSrc);
						//document.title = thisEl.attr("id") + " - " + saveSrc;
						
						$(".eachpost").removeClass("threecols");
						thisEl.addClass("threecols");
						window._gaq.push(['_trackPageview', $(".urller",thisEl).val()]);
						
						window._gaq.push(['_trackEvent', 'Items', 'Open', $(".urller",thisEl).val()]);
						var images = $(".loadImages",thisEl).attr("value").split("width=140").join("width=440");
						$(".imageArea",thisEl).html(images);
						document.title = "O.K. PERIODICALS / "+$(".titler",thisEl).val();							
						arrange();
					
					}	
				}else{
					preload_image_object.src = image_url[i].split("width=140").join("width=440");
		   		}
		   	}
			return false;
		}
	});
	
	
	function openFirstItem(){
		$("#left .eachpost:first").trigger("click");
	}
	
	function openLastItem(){
		$("#left .eachpost:last").trigger("click");
	}
	
	
	
	
