// interval vars
var clicked = 0;
var pause = false;
var bg = 0;

$(document).ready(function () { 
							
	//preload buttons						
	$('.bt').each(
	function() {
	var src = $(this).attr('src');
	var srcon = src.replace('.png','-on.png');
	pic= new Image(); 
    pic.src=srcon;
	});
	
	$('#panelbg').css('height', '326px' );	

	$('#latest').hover(
		function() { pause = true; },
		function() { pause = false; }
		)

    $("h3.tabRight").click(function() {
	myid = $(this).attr("id").substr(4,2);
	$("h3.tabRight").removeClass("active");
	$(this).addClass("active");
	//alert(myid);
	$("#rw").hide();
	$("#la").hide();
	$("#"+myid).show();
	switch(myid) {
		case "rw":
		$("#linkall > a").attr('href', 'http://www.riskwatchdog.com/podcast');
		$("#linkall > a").attr('target', '_blank');
		break;
		case "la":
		$("#linkall > a").attr('href', 'http://store.businessmonitor.com/article/');
		$("#linkall > a").attr('target', '');
		break;
	}
	});
	
	$('.icons > td > a').hover(function() {
    var myx = $(this).css("background-position");
	if(typeof(myx) === "undefined") { myx = $(this).css("background-position-x");myy = parseFloat($(this).css("background-position-y")); }
	else {
	getx = myx.split(" ");
	myx = getx[0];
	myy = getx[1];
	myy = parseFloat(myy);
	}
	$(this).css({"background-position": myx+" "+(myy-128)+"px"});	
	},
	function() {
	var myx = $(this).css("background-position");
	if(typeof(myx) === "undefined") { myx = $(this).css("background-position-x");myy = parseFloat($(this).css("background-position-y")); }
	else {
	getx = myx.split(" ");
	myx = getx[0];
	myy = getx[1];
	myy = parseFloat(myy);
	}
	$(this).css({"background-position" : myx+" "+(myy+128)+"px"});		
	});
	
	// rollover buttons
	$('.bt').live("mouseover",function () {
		var src = $(this).attr('src');
		var srcon = src.replace('.png','-on.png');
		$(this).attr('src',srcon);
	});
	$('.bt').live("mouseout",function () {
	var src = $(this).attr('src');
	var srcoff = src.replace('-on','');
		$(this).attr('src',srcoff);
	});
	
	$('#q').attr({'autocomplete':'off'});
	
	$('#viewall').click(function() {
	$('.hidden').slideToggle();
	if ($(this).html() == "View all 22 sectors") {
	 $(this).html("Minimise");
	$('#panelbg').css('height', '530px' ); }
	 else { $(this).html("View all 22 sectors");
	 $('#panelbg').css('height', '326px' );}
	})
	
	//get RW articles (from BMO)
	$.ajax({
		url: "/cgi-bin/web_api_top_stories.pl",
		type: "GET",
		cache: false,
		data: "source=bmo&output=json&count=6",
		dataType: 'json',
			error: function(jqXHR, textStatus, errorThrown){
			$("#la").html('Sorry, content unavailable.');
		},
		success: function(data){
			var text = "";
			$.each(data, function(i,item){
				//var url = "/cgi-bin/request.pl?view=articleviewer&article="+item.articleid+"&service="+(item.service ? item.service : "all")+"&iso="+(item.iso ? item.iso : "+G");
				var url = "http://store.businessmonitor.com/article/"+item.articleid+"/";
				text += "<li><a href='"+url+"' title='"+item.description+"'>"+item.Headline2+"</a></li>";
			});
			
			$("#la").html(text);
		}	
	});
	
	//get podcasts
	$.ajax({
      url: "/rw/getpodcasts.php",
      type: "GET",
	  cache: false,
	  data: "num=5",
	  error: function(msg){
       $("#rw").html('Sorry, content unavailable.');
      },
      success: function(msg){
       $("#rw").html(msg);
      }
   }
);
	
	//get latest products (From store)
	$.ajax({
		url: "/cgi-bin/web_api_top_stories.pl",
		type: "GET",
		cache: true,
		data: "source=store",
		dataType: 'json',
		error: function(jqXHR, textStatus, errorThrown){
			$("#lp").html('Sorry, content unavailable.');
		},
		success: function(data){
			var text = "";
			$.each(data, function(i,item){
				text += "<li class='cover"+item.type+"'><div class=\"pricing\"><p class=\"was\"><a href=\""+item.pretty_url+"\"><img src=\"http://store.businessmonitor.com/images/default/basketIcon.gif\" alt=\"Buy\" border=\"0\" /></a></p>"+
					"</div>"+
					"<h3><a href='"+item.pretty_url+"' >"+item.title+"</a></h3>"+
					"<div class=\"clear\"></div>"+
					"</li>";
			});
			
			$("#lp").html(text);
	
			$('#latest').jCarouselLite({
				vertical: true,
				auto: 3000,
				speed: 800,
				visible: 3
			});
		}
	});
	
	// tabs navigation
	$('#panel').tabs();
	$('#panel').tabs('option', 'fx', { opacity: 'toggle',duration: 80 });
	$('#panel').tabs( 'rotate' , 8000);
	
	$('#tabs > li > a').click(function() {
	clicked=1;
	mytab = $(this).attr('href');
	newCookie('tab',mytab,1);
	if($('#viewall').text()=="Minimise") $('#viewall').trigger('click');
	});
	
	$('#panelbg').hover(
	function() {
		$('#panel').tabs( 'rotate' , 0);
	},
	function() {
		if(clicked==0) {
		$('#panel').tabs( 'rotate' , 8000 );
		}
	});
	
	if(readCookie('tab')!="") {
	clicked=1;
	$('#panel').tabs( 'rotate' , 0);
	$('#panel').tabs('select',readCookie('tab'));
	}
						   


	// navbar stuff
	$("#navbar li > a").mouseover(function(){	
		$(this).parent().parent().find(".active").hide().removeClass("active");
		$(this).next('div').bgiframe().show().addClass("active");		
	});
	
	$('#nav_countries li > a').hover(
		function() {
		$(this).parent().parent().addClass('noBorder');	
		},
		function() {
		$(this).parent().parent().removeClass('noBorder');	
		});
		
	
	
	$("#navbar").bind("mouseleave",function(){		
		hideMenus();
	});
	
	
	$("#navbar div.column").bind("mouseleave",function(e){
		if(!$(e.target).parent().find(".active"))
			hideMenus();		
	});

	$(document).click(function(event){ 		
			var target = $(event.target);			
			if (target.parents("#navbar").length == 0) {	
				hideMenus();
			}
	});		   

});


function formSubmit() {
if (document.bmologin.checker.checked) { toMem(); }
else { delMem(); }
document.bmologin.pwd.value = hex_md5( document.bmologin.pwd.value );
return;
}

function searchSubmit() {
if ($('#q').val() !="" && $('#q').val() != "Enter Country or Industry") {
	return;
}
else { return false }
}

function show(l) {
$('#'+l).modal({opacity:60});
}

//navbar functions
function hideMenus(){
	$("#navbar div").removeClass("active");
	$("#navbar div.row, #navbar div.column").hide();
}

$.fn.bgIframe = $.fn.bgiframe = function(s) {
	// This is only for IE6
	if ( $.browser.msie && $.browser.version <7 ) {
		s = $.extend({
			top     : 'auto', // auto == .currentStyle.borderTopWidth
			left    : 'auto', // auto == .currentStyle.borderLeftWidth
			width   : 'auto', // auto == offsetWidth
			height  : 'auto', // auto == offsetHeight
			opacity : true,
			src     : 'javascript:false;'
		}, s || {});
		var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
		    html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
		               'style="display:block;position:absolute;z-index:-1;'+
			               (s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
					       'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
					       'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
					       'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
					       'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
					'"/>';
		return this.each(function() {
			if ( $('> iframe.bgiframe', this).length == 0 )
				this.insertBefore( document.createElement(html), this.firstChild );
		});
	}
	return this;
};

function clearme() {
	if($('#q').val()=="Enter Country or Industry") {
		$('#q').val("");
		$('#q').css({'color':'#222'});
	}
}
function clearEmail() {
	if($('#email').val()=="Enter your email") {
		$('#email').val("");
		$('#email').css({'color':'#222'});
	}
}

jQuery.fn.outerHTML = function(s) {
return (s)
? this.before(s).remove()
: jQuery("<p>").append(this.eq(0).clone()).html();
}

function newCookie(name,value,days) {
 var days = 30;   // the number at the left reflects the number of days for the cookie to last
                 // modify it according to your needs
 if (days) {
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString(); }
   else var expires = "";
   document.cookie = name+"="+value+expires+"; path=/"; }

function readCookie(name) {
   var nameSG = name + "=";
   var nuller = '';
  if (document.cookie.indexOf(nameSG) == -1)
    return nuller;

   var ca = document.cookie.split(';');
  for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameSG) == 0) return c.substring(nameSG.length,c.length); }
    return null; }

function eraseCookie(name) {
  newCookie(name,"",1); }

function toMem(a) {
    newCookie('theEmail', document.bmologin.uid.value);   // field you wish to have the script remember
}

function delMem(a) {
  eraseCookie('theEmail');
  }


function remCookie() {
document.bmologin.uid.value = readCookie("theEmail");
if(readCookie("theEmail")!="") {
	$('#checker').attr('checked', true);
}
else { $('#checker').attr('checked', false); }
}

function refreshFilters() {
	$.ajax({
		url: "/local/store catalogue/store/getFilters.php",	
		type: "GET",
		cache: true,
		data: $("#filters").serialize(),
		dataType: 'html',
		error: function(data){
			$("#storefilters").html('Sorry, content unavailable.');
		},
		success: function(data){			
			$("#storefilters").html(data);
		}
	});
}

function gotostore() {
	window.location.href = "/local/store catalogue/store/"+document.filters.g.value+"/"+document.filters.i.value+"/"+document.filters.p.value+"/";
}
