/////////////////////////////////////////////////////////////////
// FileName : common.js
// Description : Common JavaScript scripts for application
// This file will always be loaded by the bootstrap's page
// so it is accessible by all pages and modules
/////////////////////////////////////////////////////////////////

// initialize the page level events
$(document).ready(function() {

	// sign in form
	$("#signInLink").click(function() {
		$("a#signInLink").addClass("showTab");
		$("#quickSignInFormWrapper").fadeIn("normal");
	});
	$("#quickSignInForm a.close").click(function() {
		$("#quickSignInFormWrapper").hide();
		$("a#signInLink").removeClass("showTab");
	});
	
	// forgot password link
	$("a.forgotPasswordLink").click(function () {
			$("#consumerForgotPassword").modal({
				containerId: "consumerForgotPasswordModalContainer",
				close: false
			});
		} 
	);
	
	$(".messageBody .chatDeclineLink").click(function() { 
		$("#messageWrapper").fadeOut("fast");
	});
	$(".messageBody .chatAcceptLink").click(function() { 
		$("#messageWrapper").fadeOut("fast");
	});
	
	// add input hint logic to all input fields with 
	// a class of inputHint
	// ... focus().blur() => fix for firefox autocomplete
	var $fvii = $("form.validate input.inputHint");
	if( $fvii.length ) {
		$fvii.inputHint().focus().blur();
	}
	
	// Fix map png images in IE6
	if ($.isFunction($.pngFix)) {
		$("div.MSVE_MapContainer").pngFix();
	}

	var selector = ".loading[id^=profilelist_]";
	if ($(selector).length) {
		$(window).bind("scroll", {_selector: selector, _container: window, _threshold: 100}, llProfileImages).trigger("scroll");
	}

});

/**
* openWin
* Client side script to open a new browser window
*/
function openWin(mypage,w,h,scroll) {
	myname='extra';
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars  = '+scroll+',resizable'
	win = window.open(mypage,myname,settings)
}

/**
* openChatWindow
* function to open a chat window
*/
function openChatWindow(chaturl) {
	window.open(chaturl, 'chatwin', 'toolbar=0,location=0,directories=0,menubar=0,status=0,scrollbars=no,resizable=0,width=180,height=370,top=0,left=0');
}

/**
* selectMyToolsTab
* function to handle mytools page navigation
*/
function selectMyToolsTab( name ){
	var path = window.location.pathname;
	var currentPage = path.substring( path.lastIndexOf( '\\' ) + 1 );
	
	myToolsPageName = '/mytools/';
	
	if ( currentPage == myToolsPageName ){
		$( "#mytoolsTabsWrapper > ul" ).tabs( "select", name );
	} else {
		window.location = myToolsPageName + name;
	}
}

/**
* URL encode the given value
*/
function encodeString(string) {
	string = string.replace(/^[\s]+|[\s]+$/g, "");
	string = string.replace(/\s/g, "+");
	return string;
}

/**
 * lazy load profile images via ajax
 */
function llProfileImages(event) {
	var items = $(event.data._selector);
	var container = event.data._container || window;
	var threshold = event.data._threshold || 0;

	// viewport
	var viewport = (container === window)
	             ? { top: $(window).scrollTop(), left: $(window).scrollLeft() }
	             : $(container).offset();
	viewport.bottom = viewport.top + $(container).height();
	viewport.right = viewport.left + $(container).width();

	// search for items in view
	var ids=[];
	$(items).each(function(i, elem) {
		elem = $(elem);

		// skip those already downloading
		if(elem.hasClass("inprog")) return;
	
		// top left bottom right
		// ... of loading image container e.g. the box
		var box = elem.parent(), tlbr = box.offset();
		tlbr.top = Math.max(0, tlbr.top - threshold);
		tlbr.left = Math.max(0, tlbr.left - threshold);
		tlbr.bottom = tlbr.top + box.height() + threshold;
		tlbr.right = tlbr.left + box.width() + threshold;

		// in view - add id to list of images to get
		if(viewport.top < tlbr.bottom && viewport.left < tlbr.right && viewport.bottom > tlbr.top && viewport.right > tlbr.left) {
			ids.push(elem.addClass("inprog").attr("id").replace(/^profilelist_/, ""));
		}
	});

	// found some ids - 1 call per scroll!
	if(ids.length) {
		$.ajax({
			async: true,
			url: "/ajax/profile/getimages/profilelist/"+ ids.join(",") +"/?nd=" + (new Date).getTime(), // new query every time, no caching...???
			type: "GET",
			dataType: "text", // for envelope
			success: function(data) {
				data = new Envelope(data);
				data = data.payload.images || [];

				for(var i=0,n=data.length; i<n; i++) {
					if(data[i] && data[i].id && data[i].src) {
						$("#profilelist_" + data[i].id).attr("src", data[i].src).removeClass("loading inprog");
					}
				}
			}
		});
	}
}

