﻿var $j = jQuery.noConflict();
$j(document).ready(function () {

		//write in the fancybox CSS
		//var link = $j("<link>").attr({type: 'text/css',rel: 'stylesheet',href: 'http://webarchive.nationalarchives.gov.uk/+/http://decc.gov.uk/scripts/modal/jquery.fancybox-1.3.1.css'});
		//$j("head").append(link); 	

			$j("a").focus(function(){
			//if ($j("img", this).length ==0 ){
				$j(this).toggleClass("focus")
			//	}
			})
			
			$j("a").blur(function(){
			//if ($j("img", this).length ==0 ){
				$j(this).toggleClass("focus")
			//	}
			})
			$j("a").mousedown(function(){
			//if ($j("img", this).length ==0 ){
				$j(this).unbind("focus").removeClass("focus")
			//	}
			})
			
		$j("body a").each(function(){
			if (this.hostname && (this.hostname !== location.hostname) && ($j("span.external",this).length ==0) && (this.href.indexOf("javascript") == -1) &&(this.href.length != -1)){
			if($j(this).parent().parent().parent(".Nav-x").length ==0){
					$j(this).attr("target","_new")
					var $title = ""
					var $title = $j(this).attr("title")
					$j(this).attr("title", $title + " (opens in a new tab or window)")
					if ($j("img", this).length ==0 ){
						$j(this).append("<span class=\"external\"><span class=\"hide\"> [External link]</span></span>")
					}
					
				}
			}
			//var $href= $j(this).attr("href")
			//	$j(this).attr("href", $href + "&minwidth=true")
		})
		/*Minimum width for popup windows*/
		
		
		//switch to .pdf,.csv,.xls,.xlsx,.doc,.docx,.zip,.ppt,.pptx when live
		$j("a[href*='viewfile.ashx']").each(function(){
			if (($j("span.download",this).length ==0) && ($j("span.external",this).length ==0)){
				$j(this).append("<span class=\"download\"><span class=\"hide\"> [Document link]</span></span>")
				.attr("href", function(){return this.href + "&minwidth=true"})
				
				.fancybox({
					'titleShow': false,'centerOnScroll':true,'type':'iframe','onClosed':function(){window.scrollTo(0,0);refreshBasket(0)}
				});
				getfileinfo(this);
				
			}
		})
			
			
		var urlpos = gup('minwidth');
		if (urlpos != "") $j("body").addClass("minwidth");	
			//.click(function(event) {
		 //event.preventDefault();
		//$j(this)
		//})
	
})
function getfileinfo(target){

	$j.ajax({
		url: 'http://webarchive.nationalarchives.gov.uk/+/http://decc.gov.uk/publications/basket.aspx?filepath='  +target.href + '&checkorder=true',
		success: function(data) {
			if (data.indexOf("True") != -1){
				$j("span.download",target).addClass('order')
				$j("span.download span",target).html(function(){return " [" + data.replace("True","") +"]"})
				var stitle = $j(target).text();
				$j(target).attr("title","(Opens in new browser window. Click TAB to navigate or ESC to close) '" + stitle + "'. Available in digital and printed format.")
				}
				else{
				$j("span.download span",target).html(function(){return " [" + data.replace("False","")+ "]"}).attr("title","This publication is available in digital format only")
				var stitle = $j(target).text();
				$j(target).attr("title","(Opens in new browser window. Click TAB to navigate or ESC to close) '" + stitle +". ' Available in digital format only.")
				}
			}
		});
}

function gup(name,source) {
		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regexS = "[\\?&]"+name+"=([^&#]*)";
		var regex = new RegExp( regexS );
		var results = regex.exec(source);
		if (results == null) return "";
		else return results[1];
	}
function updateBasket(itemCount){
if (itemCount = 0){
itemCount = $j(".basCount").html().substring(0,1)+1
}
$j(".basket").animate({opacity: 0.25,top:'-25px'}, 700, function() {$j(".basCount").html(itemCount +' items');$j('.basket').delay(250).animate({opacity: 1,top:'0px'}, 500).addClass('trigger')}
  
  )};
function refreshBasket(itemCount){
  $j.ajax({
		url: 'http://webarchive.nationalarchives.gov.uk/+/http://decc.gov.uk/publications/basket.aspx?total=true',
		success: function(data) {
		itemCount = data;

		}
	});
	if (itemCount != 0){
		$j(".basket").animate({opacity: 0.25,top:'-25px'}, 700, function() {$j(".basket").css("display","block");$j(".basCount").html(itemCount +' items');$j('.basket').delay(250).animate({opacity: 1,top:'0px'}, 500).addClass('trigger')})
	 }
};



