if(typeof console == "undefined") {
	console = {
		log : function(logData) {
			//alert(logData);
		}
	};
}

function scrollToTop() {
	jQuery('html').scrollTop('0px');
	jQuery('body').scrollTop('0px');
}

function doDelayClick(url) {
	setTimeout(
			function() {
				location.href=url
			}, 500);
}

function getViewPortSize() {
	var viewportwidth;
	var viewportheight;
	 
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	} else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return {'width' : viewportwidth, 'height' : viewportheight};
}

function roll_image() {
	jQuery('img.roll_image').hover(function() {
			var imageSrc = jQuery(this).attr("src");
			imageArr = imageSrc.split(".");
			// If roll is set replace it
			imageArr[imageArr.length - 2] = imageArr[imageArr.length - 2].replace("_roll","");
			// Now add roll
			imageArr[imageArr.length - 2] = imageArr[imageArr.length - 2]+"_roll"
			var newImg = "";
			for(var i=0;i<imageArr.length;i++) {
				newImg += imageArr[i];
				if(i < imageArr.length-1) {
					newImg += ".";
				}
			}
			jQuery(this).attr("src", newImg);
		}, function() {
			var imageSrc = jQuery(this).attr("src");
			imageArr = imageSrc.split(".");
			imageArr[imageArr.length - 2] = imageArr[imageArr.length - 2].replace("_roll","");
			var newImg = "";
			for(var i=0;i<imageArr.length;i++) {
				newImg += imageArr[i];
				if(i < imageArr.length-1) {
					newImg += ".";
				}
			}
			jQuery(this).attr("src",newImg);
	});
}

function togglePopupNoBackground(id, showHide) {
    if (showHide) {
        jQuery("#" + id).show().center();
    } else {
        jQuery("#" + id).hide();
    }
}

function rolloverItem(classElement) {
	jQuery('.'+classElement)
		.hover(
			function() {
				jQuery(this).addClass(classElement+'_roll');
			}, 
			function() {
				jQuery(this).removeClass(classElement+'_roll');
			});
}

function toggleTab(divId, divClasses) {
		jQuery('.'+divClasses).css('display', 'none');
		jQuery('#'+divId).css('display', 'block');
}

function getTimezoneOffsetInMsec() {
	return (new Date()).getTimezoneOffset() * 60000;
}

jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}

jQuery(document).ready(function(){
	rolloverItem('tab');
	roll_image();
	if (typeof jsonlogininfourl != 'undefined') {
		genericGetLoggedInInfo();
	}
});

function genericGetLoggedInInfo() {
	var url= jsonlogininfourl+"?time="+(new Date()).getTime();
	var params={};
	if (typeof forumFlag != 'undefined') {
		params={forumFlag:forumFlag};
	}
    jQuery.ajax({
      type: "GET",
      dataType: "json",
      url: url,
      data: params,
      cache: false,
      success: onGenericGetLoggedInInfo,
      error: ajaxErrorHandler
    });
}

function onGenericGetLoggedInInfo(data) {
    
	//TODO this is a hack to do this without a deployment, should really be done in the PSNAjaxController.
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		data.isNoLoginIframe = true;
	}
	
	if (typeof onGetHeaderLoggedInInfo != 'undefined' && onGetHeaderLoggedInInfo instanceof Function) {
    	onGetHeaderLoggedInInfo(data);
    }
    if (typeof onGetFooterLoggedInInfo != 'undefined' && onGetFooterLoggedInInfo instanceof Function) {
    	onGetFooterLoggedInInfo(data);
    }
    if (typeof onFetchLoggedInInfo != 'undefined' && onFetchLoggedInInfo instanceof Function) {
    	onFetchLoggedInInfo(data);
    } 
    if (typeof onFetchBannerLoggedInInfo != 'undefined' && onFetchBannerLoggedInInfo instanceof Function) {
    	onFetchBannerLoggedInInfo(data);
    } 
}

function MessageSource(source) {
	this.source = source;
	
	this.get = function(key, values) {
		var retVal = source[key];
		if (!retVal) {
			return "!!!" + key + "!!!";
		}
		if (values && values instanceof Array) {
			for (var i = 0; i < values.length; i++) {
				var pattern = new RegExp('\\{' + i + '\\}', 'g');
				retVal = retVal.replace(pattern, values[0]);
			}
		}
		return retVal;
	};
}

function ajaxErrorHandler(xhr, textStatus, errorThrown) {
	if (xhr) {
		if (xhr.status == 0 && textStatus == "parsererror") {
			return;
		} else {
			alert("error: " + xhr.status + " " + textStatus);
		}
	}
}

var searchController = function() {
	var priv = {
			searchInputFieldId: "",
			ajaxServletPath : "",
			searchString: "",
			popupContainerId : "",
			orderBy: "name",
			orderAsc: true,
			searchInProgress: false,
			minCharactersWarning: "",
			showResult : function(data, textStatus, XMLHttpRequest) {
				if (!data) {
					return;
				}
				if (data.redirect) {
					location.href = data.redirect;
					return;
				}
				if (data.html) {
					//consoleLog.log(data.html);
					jQuery("#" + priv.popupContainerId).empty().append(data.html).show().center();
					if(isPS3) {
						jQuery("#" + priv.popupContainerId).css('top', '680px');
						
					}
					// jQuery("#" + priv.popupContainerId).empty().append("<div style='background-color: #FF0000; with: 300px; height: 300px;'>Bla<\/div>").show().center();
				}
				priv.searchInProgress = false;
			},
			showError : function(request, textStatus, errorThrown) {
				console.log.log(request);
			},
			showLoadingInfo : function(showHide) {
			    if (showHide) {
			    	var html = jQuery('#loading_template_search').html();
			    	jQuery("#" + priv.popupContainerId).empty().html(html).show().center();
			    } else {
			    	jQuery("#" + priv.popupContainerId).empty().hide();
			    }				
			},
			doExecuteSearch : function() {
		        jQuery.ajax({url: priv.ajaxServletPath,
		        	cache : false,
		            data: {name : priv.searchString, orderby: priv.orderBy, asc: priv.orderAsc},
		            success: priv.showResult,
		            error: function(request, textStatus, errorThrown) {
		                priv.showLoadingInfo(false);
		                priv.showError(request, textStatus, errorThrown);
		                priv.searchInProgress = false;
		                },
		            dataType: "json"});
			}
	};
	return {
		init : function(searchInputFieldId, ajaxServletPath, popupContainerId) {
			priv.searchInputFieldId = searchInputFieldId;
			priv.ajaxServletPath = ajaxServletPath;
			priv.popupContainerId = popupContainerId;
		},
		executeSearch : function() {
			var searchString = jQuery("#" + priv.searchInputFieldId).val();
			priv.searchString = searchString;
			if(priv.searchString.length < 3) {
				alert(priv.minCharactersWarning);
				return;
			}						
			priv.orderBy = "name";
			priv.orderAsc = true;
			priv.showLoadingInfo(true);
			// Hardcoded as temp fix
			if(priv.searchInProgress) {
				alert("Search already in progress");
				return;
			}
			priv.searchInProgress = true;
			setTimeout(function() {
				priv.doExecuteSearch();
			}, 500);
		},
		updateSearch : function(orderBy) {
			var searchString = jQuery("#" + priv.searchInputFieldId).val();
			priv.searchString = searchString;
			if(priv.searchString.length < 3) {
				alert(priv.minCharactersWarning);
				return;
			}			
			if (orderBy == priv.orderBy) {
				priv.orderAsc = !priv.orderAsc;
			} else {
				priv.orderAsc = true;
			}
			priv.orderBy = orderBy;
			priv.doExecuteSearch();
		},
		setminCharactersWarning : function(minCharactersWarning) {
			priv.minCharactersWarning = minCharactersWarning;
		}
	};
}();
function openInfoPopup(id) {
	jQuery('#' +id).show();
}
function closeInfoPopup(id) {
	jQuery('#' +id).hide();
}
