/* Add per-project common javascript functions. */
jQuery( document ).ready( function(){
	var productArr = ["external", "new_popup", "popup", "pdf"];
	jQuery.each( productArr, function(i) {
		switch (i) {
			case 0 : jQuery("a[rel='external']").click(function() {
							window.open(this.href);
							return false;
						});
						break;
			case 1 : jQuery("a[rel='new_popup']").click(function() {
							var delim = (this.href.indexOf('?') >= 0) ? '&' : '?';
							var h = parseInt(getParameterByNameFromURL('height', this.href));
							var w = parseInt(getParameterByNameFromURL('width',  this.href));
							if (!h) { h = 500; }
							if (!w) { w = 700; }
							var features = "height=" + h + ",width=" + w + ",menubar=0,status=0,location=0,toolbar=0,scrollbars=yes,resizable=yes";
							newwindow=window.open(this.href , 'newPopup', features);
							return false;
						});
						break;
			case 2 : jQuery("a[rel='popup']").click(function() {
							var delim = (this.href.indexOf('?') >= 0) ? '&' : '?';
							var h = parseInt(getParameterByNameFromURL('height', this.href));
							var w = parseInt(getParameterByNameFromURL('width',  this.href));
							if (!h) { h = 600; }
							if (!w) { w = 870; }
							var features = "height=" + h + ",width=" + w + ",menubar=0,status=0,location=0,toolbar=0,scrollbars=yes,resizable=yes";
							newwindow=window.open(this.href , 'newPopup', features);
							return false;
						});
						break;
			case 3 : jQuery("a[rel='pdf']").click(function() {
							if( this.href.indexOf('.pdf') > 0 && typeof ntptEventTag == 'function' ) {
								ntptEventTag('ev=pdf_open&evdetail=' + escape(jQuery(this)[0].href));
							}
							window.open(this.href);
							return false;
						});
						break;
			default : jQuery("a[rel=" + productArr[i] + "]").click(function() {
							var delim = (this.href.indexOf('?') >= 0) ? '&' : '?';
							var h = parseInt(getParameterByNameFromURL('height', this.href));
							var w = parseInt(getParameterByNameFromURL('width',  this.href));
							if (!h) { h = 400; }
							if (!w) { w = 500; }
							var features = "height=" + h + ",width=" + w + ",menubar=0,status=0,location=0,toolbar=0,scrollbars=yes,resizable=yes";
							var productVar = ( i == 1 ) ? "" : productArr[i];
							newwindow=window.open(this.href + delim + 'popup=true#' + productVar, 'Popup', features);
							return false;
						});
		}
	});
	jQuery("a[rel='lightbox']").click(function() {
		jQuery("#lightbox-content").addClass(jQuery(this).attr("class")).get(jQuery(this).attr("href"));
		jQuery("#lightbox-container").fadeIn(500);
		return false;
	});
	jQuery("a[rel='lightbox']").click(function() {
		var link = jQuery(this).attr("href")
		var h = parseInt(getParameterByNameFromURL('height', link));
		var w = parseInt(getParameterByNameFromURL('width',  link));
		if (!h) { h = 400; }
		if (!w) { w = 500; }
		if(link.indexOf("?")!=-1)
			link = link+"&popup=true&lb=true"
		else
			link = link+"?popup=true&lb=true"
		jQuery("#lightbox-border").addClass(jQuery(this).attr("title"));
		jQuery("#lightbox-border").prepend('<div class="lightbox-tl"><\/div><div class="lightbox-top"><\/div><div class="lightbox-tr"><\/div><div id="lightbox-close"><\/div><div class="clear-both"><\/div><div id="lightbox-content"><\/div>');
		jQuery("#lightbox-border").append('<div class="lightbox-bl"><\/div><div class="lightbox-bottom"><\/div><div class="lightbox-br"><\/div>');
		if(jQuery(this).attr("href").indexOf("http") != "-1"){
			if(jQuery(this).attr("title") == "lightbox-video"){
				jQuery("#lightbox-content").html("<iframe src='"+link+"' width='500' height='360' frameborder='0' style='margin: 0 20px;'><\/iframe>");
			}else {
				jQuery("#lightbox-content").html("<iframe src='"+link+"' width='"+w+"' height='"+h+"' frameborder='0' style='margin: 0 20px;'><\/iframe>");
			}
		}else{
			jQuery("#lightbox-content").load(jQuery(this).attr("href")+" .lightbox");
			if(jQuery(this).attr("href").indexOf("/video_pop?video=brcv") != "-1"){
				setTimeout("brightcove.createExperiences()", 1500);
			}
			if(jQuery(this).attr("href").indexOf("/locale-selector") != "-1"){
				setTimeout("appendLocales()", 2000);
			}
		}
		if( typeof ntptEventTag == 'function' ) {
			ntptEventTag('ev=lightbox_open&evdetail=' + escape(jQuery(this)[0].href));
		}
		jQuery("#lightbox-container").fadeIn(100);
		jQuery("#lightbox-close").hover(function(){ jQuery(this).css('cursor','pointer') });
		setTimeout("lightboxLinks()", 2000);
		jQuery("div#lightbox-close").click(function() {
			lightboxClose();
		});
		return false;
	});
	jQuery('.tabs li').hover(
		function(){
			jQuery(this).mouseover(function(){
				jQuery(this).addClass('hover');
			})},
		function(){
			jQuery(this).mouseout(function(){
				jQuery(this).removeClass('hover');
			})
		}
	);
	jQuery('a.tooltip').hover(function(){
		var tooltip = this.href.substring(this.href.indexOf('#'))
		jQuery(tooltip).show();
	}, function(){
		var tooltip = this.href.substring(this.href.indexOf('#'))
		jQuery(tooltip).hide();
	});
});
function appendLocales(){
	var localeArr = ["loc_com", "loc_de", "loc_co.uk", "loc_fr", "loc_com.au", "loc_ca", "loc_fr-ca"];
	jQuery.each( localeArr, function(i) {
		jQuery("a[rel=" + localeArr[i] + "]").click(function() {
			this.href = 'http://'+serverName+'gotoassist.com/remote_support/?location='+localeArr[i].substring(localeArr[i].indexOf('_')+1);
		});
	});
}
function lightboxClose(){
		jQuery("#lightbox-container").fadeOut(100, function(){
		jQuery("#lightbox-border").removeClass().html("");
		jQuery("#lightbox-container").hide();
	});
}
function lightboxLinks(){
	jQuery("#lightbox-content a[rel='external']").each(function(){
		jQuery(this).click(function(){window.open(jQuery(this).attr('href')); return false;});
	});
}
function getParameterByNameFromURL(name, url) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(url);
	if(results == null) {
		return "";
	}
	else {
		return results[1];
	}
}
function preFillElements(theObject, flag) {
	if(flag) {
		for(i=0;i<theObject.length;i++) {
			theTarget = document.getElementById(theObject[i][0]);
			if(theTarget.value == "")
				theTarget.value = theObject[i][1];
			(flag == "prefill" && ( theTarget.value == theObject[i][1] ) ) ? eventHandlerClass('add', theTarget, flag) : clearElement(theTarget, theObject[i][1]);
		}
	}
	return true;
}
function clearElement(theObject, defaultText, refill) {
	theAction = ( refill && ( ( theObject.value == ""	) || ( theObject.value == defaultText ) ) ) ? 'add' : 'remove';
	eventHandlerClass( theAction, theObject, 'prefill' );
	if( refill && theObject.value == "" )
		theObject.value = defaultText;
	else if( theObject.value == defaultText )
		theObject.value = "";
}
function eventHandlerClass(theAction,theObj,class1,class2) {
	switch( theAction )	{
		case 'add':
			if(!eventHandlerClass('check',theObj,class1)){theObj.className+=theObj.className?' '+class1:class1;}
			break;
		case 'remove':
			var rep = theObj.className.match(' '+class1) ? ' ' + class1 : class1;
			theObj.className = theObj.className.replace(rep, '');
			break;
		case 'check':
			return new RegExp('\\b'+class1+'\\b').test(theObj.className)
			break;
	}
}
