// -------------
//--- JQUERY ---
// -------------
$(document).ready(function () { //HTML DOM document is ready
	
//    $.ajaxSetup({
//        // Disable caching of AJAX responses */ 
//        cache: false
//    });
							
	//find it open / close
	$('a.findit, #findit ul.closebtn a').live("click", function() {		
		var linkcontent = $(this).html();
		if ($("#findit").length != 0) {
			showHideFindIt($("#findit"), linkcontent);
		} else {
			$("#navigation").after('<div id="findit" class="container_20">&nbsp;</div>');
			$("#findit").hide().load($(this).attr("href") + "  #findit > *", function() {
				$('#myselectbox').selectbox({debug: true});
				showHideFindIt($(this), linkcontent);
			 });
		};
		if ($("li.findit").hasClass("finditlit")) {
			$("li.findit").removeClass("finditlit");
		} else {
			$("li.findit").addClass("finditlit");
		}
		return false;
	});
	
	$("input[type=text]").bind('focus',function() {  // only select if the text has not changed
	  if(this.value == this.defaultValue) {
		  this.select();
	  }
	});
	
	$('div.findmenu a').live("click", function() {
		var findpanel = $(this).html().replace(" ", "").toLowerCase();
		window.location.hash = findpanel;
		loadpanel($(this).attr("href"));
		return false;
	});
															 
	//sharethis open / close
	$('a.sharethis, #sharethis ul.closebtn a').live("click", function() {
		var linkcontent = $(this).html();
		if ($("#sharethis").length != 0) {
			showHideShareThis($("#sharethis"), linkcontent);
		} else {
			$("#content").before('<div id="sharethis">&nbsp;</div>');		
			$("#sharethis").hide().load($(this).attr("href") + "  #sharethis > *", function() {
				//$('#myselectbox').selectbox({debug: true});
				showHideShareThis($(this), linkcontent);
			 });
		};
		return false;
	});


	//image rollover
	$("a.roll").live('mouseover focus', function(event) {
		$(this).find('img').attr("src", $(this).find('img').attr("src").split('_f1').join('_f2'))
	}).live('mouseout blur', function(event) {/*mouseout*/
		$(this).find('img').attr("src", $(this).find('img').attr("src").split('_f2').join('_f1'))
	});

	//image rollover
	$("a.rollbg, a.rollpngbg").live('mouseover focus', function(event) {
		$(this).css('background-image', $(this).css('background-image').split('_f1').join('_f2'));
	}).live('mouseout blur', function(event) {/*mouseout*/
		$(this).css('background-image', $(this).css('background-image').split('_f2').join('_f1'));
	});

	//active tooltip
	$("a.activatetooltip").live('mouseover focus mouseout blur', function(event) {
		$(this).next('div.tooltip').toggleClass("displayblock");
	});
	
	//table row rollover
	$.each(["table.financial tbody tr", "table.broker tbody tr","table.info:not(.nohover) tbody tr:not(.nohover)","table.newspanel tbody tr","table.news tbody tr"], function(index, value) { 
  		$(value).live('mouseover mouseout', function() {
			$(this).toggleClass("hover");
		});
	});

	//video rollover
	$(".videothumb").live('mouseover mouseout', function() {
		$(this).find(".bgblock_border").toggleClass("highlight");
	});

	$(".detectdownload").live("click", function() {
		if( typeof(s) != 'undefined' ) {
			s.tl(true,'o','Download ' + $(this).attr("id"));
		}
	});
	//open links in a new window - rel=external
	$("a[rel=external]").live("hover focus", function() {
		if (typeof($(this).data("external")) === "undefined") {
			$(this)
				.attr("target","_blank")
				.data("external",1)
				.attr("title") !=='' ? this.title=$(this).attr("title")+'. Opens in a new browser window.' : this.title='Opens in a new browser window.';	
		}
	});
	
	//webcast popups
	$("a[rel=webcast_popup]").live("hover focus", function() {
		if (typeof($(this).data("external")) === "undefined") {
			$(this)
				.attr("target","_blank")
				.data("external",1)
				.attr("title") !=='' ? this.title=$(this).attr("title")+'. Opens in a new browser window.' : this.title='Opens in a new browser window.';	
		}
	}).live("click", function() {
		window.open($(this).attr('href'), 'popup_window', 'width=750,height=580,location=1,resizable=1,status=0,scrollbars=1').focus();
        return false;
    });

	$("a[rel=presentation_popup]").live("hover focus", function() {
		if (typeof($(this).data("external")) === "undefined") {
			$(this)
				.attr("target","_blank")
				.data("external",1)
				.attr("title") !=='' ? this.title=$(this).attr("title")+'. Opens in a new browser window.' : this.title='Opens in a new browser window.';	
		}
	}).live("click", function() {
		window.open($(this).attr('href'), 'popup_window', 'width=815,height=600,location=1,resizable=1,status=0,scrollbars=1').focus();
        return false;
    });

	//form button rollovers
	$("form button.sendbtn").live("mouseover mouseout focus blur", function() {
		$(this).toggleClass("sendbtn_f2");
	});
	$("form button.submitbtn").live("mouseover mouseout focus blur", function() {
		$(this).toggleClass("submitbtn_f2");
	});
	$("form input.button").live("mouseover mouseout focus blur", function() {
		$(this).toggleClass("button_f2");
	});
	$("form input.submit").live("mouseover mouseout focus blur", function() {
		$(this).toggleClass("button_f2");
	});
	$("form input[type='image']").live('mouseover focus', function(event) {
		$(this).attr('src', $(this).attr('src').split('_f1').join('_f2'));
	}).live('mouseout blur', function(event) {/*mouseout*/
		$(this).attr('src', $(this).attr('src').split('_f2').join('_f1'));
	});
	
	//Email alerts page
	$("#profession").selectbox();
	
	/*submit sharethis form
	$("#sharethis form .sendbtn").live("click", function(f) {
	submitShareForm($("#sharethis form"));
		return false;
	});*/
	
});	

// --------------------------------------
// --- DOWNLOAD KIT SELECT ALL BUTTON ---
// --------------------------------------
function selectDownload(){
	var intDownloads=document.getElementById('assetlist').elements.length;
	for(var i=3;i<intDownloads;i++){
		if(document.getElementById('assetlist').checkall.checked==true){
			document.getElementById('assetlist').elements[i].checked=true;
		}else{
			document.getElementById('assetlist').elements[i].checked=false;
		}
	}
}

// --------------------------------------
// --- PRESENTATIONS POPUP ---
// --------------------------------------
var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=773,height=600';

function raw_popup(url, target, features) {
if (features ==""){
	theFeatures = _POPUP_FEATURES;
}else{
	theFeatures = features
}
var theWindow = window.open(url, target, theFeatures);
theWindow.focus();
return theWindow;
}

function link_popup(src, target, features) {
	return raw_popup(src, target, features);
}

function showHideFindIt(e, linkcontent) {
	$(e).animate({height: "toggle"}, 1000);
	// metrics code starts
	if( typeof(s) != 'undefined' ) {
		s.tl(true,'o','Find it panel : page '+getParameterByName("pageid") + ' ' + linkcontent);
	}
	// metrics code ends
}

function showHideShareThis(e, linkcontent) {
	$(e).animate({height: "toggle"}, 1000, function() {
		// metrics code starts
		if( typeof(s) != 'undefined' ) {
			s.tl(true,'o','Share this panel : page '+getParameterByName("pageid") + ' ' + linkcontent);
		}
		// metrics code ends
		//New in store section updates page title when loading content
		$("#sharepagetitle").attr("value",document.title);
	}).focus();
}

//findit
function loadpanel(findpanel) {
	$(".findpanel").load(findpanel + " .findpanel > *", function() {
		// metrics code starts
		if( typeof(s) != 'undefined' ) {
			var getTab = findpanel.split("findmenu=")[1].split("#")[0];
			s.tl(true,'o','Find it panel : page '+getParameterByName("pageid") + ' : tab ' + getTab);
		}
		// metrics code ends
	});
}

//submit sharethis form
function submitShareForm(f){
	var action = f.attr("action");
	var serializedForm = f.serialize();
	//$("input.submit").attr("src", $("input.submit").attr("src").split('_f2').join('_f3'))
	$.post(action, serializedForm, function(data){
		$("#sharethis").html(data);
	});
}

//get page params for web mets
function getParameterByName(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

