
/*
 * Greek Life Online 2011
 * Gobal Jquery Lib
 */
var loadingImg = "<center><img src='/custom/lib/images/misc/loading.gif' /></center>";
var dropDownOpen = false;

var PageLoad = function() {
	this.time;
	this.activityLoads = 0;
	this.__construct = function() {
		this.time = Math.round((new Date()).getTime() / 1000);
	}
	this.updateTime = function() {
		this.time = Math.round((new Date()).getTime() / 1000);
	}
	this.getTime = function() {
		return this.time;
	}
	this.getActivityLoads = function() {
		return this.activityLoads;
	}
	this.incActivityLoads = function() {
		this.activityLoads++;
	}
	this.__construct();
}
var pageData = new PageLoad();

var RemoteCallModel = function(type, url, data, callBack) {
	this.type;
	this.data;
	this.url;
	this.callBack;
	this.__construct = function(type, url, data, callBack) {
		this.type=type;
		this.data=data;
		this.url = url;
		this.callBack = callBack;
	}
	this.getData = function() {
		return this.data;
	}
	this.getType = function() {
		return this.type;
	}
	this.getUrl = function() {
		return this.url
	}
	this.getCallBack = function() {
		return this.callBack;
	}
	this.__construct(type, url, data, callBack);
}

var RemoteServiceQueue = function() {
	
	this.coreService;
	this.queue;
	
	this.__construct = function() {
		this.queue = new Array();
	}
	
	this.add = function(data) {
		this.queue.push(data);
	}
	
	this.getQueue = function() {
		return this.queue;
	}
	
	this.execute = function() {
		$.each(this.queue, function(key, data) {
			switch(data.getType()) {
				case "json":
					new RemoteJsonCall(data.getUrl(), data.getData(), data.getCallBack());
					break;
				case "ajax":
					new RemoteAjaxCall(data.getUrl(), data.getData(), data.getCallBack());
					break;
			}
		});
	}
	
	this.__construct();
}

var RemoteAjaxCall = function(url, params, callBack) {
	$.ajax({
		type: 'POST',
		url: url,
		data: $.param(params),
		success: function(data) { if (callBack.length) { window[callBack](data); } },
		error: function(xhr, ajaxOptions, thrownError) { /* console.log(ajaxOptions); */ }
	}); 
}

var RemoteJsonCall = function(url, params, callBack) {
	$.ajax({
		type: 'POST',
		url: url,
		dataType: 'json',
		data: $.param(params),
		success: function(data) { window[callBack](data); },
		error: function(xhr, ajaxOptions, thrownError) {/*  console.log(ajaxOptions);  */}
	}); 
}

var remoteService = new RemoteServiceQueue;

$(document).ready(function() {
    $("button").button();
	
    $("#button-search").button({ icons: {primary:'ui-icon-search'}, text: false }).addClass("grk-search-button-image-only");

    $("#tabs").tabs();

	if($('#error-out').length != 0) {
		$('#error-out').hide().slideDown("slow").delay(3500).slideUp("slow");
	}
	
	$(".dropdown").click(function(){
		if ($(this).hasClass("active")) {
			$(this).removeClass("active");
			dropDownOpen = false;
		} else {
			$(this).addClass("active");
			dropDownOpen = true;
        }
    });
	
	$("button#link").each(function(){
		$(this).bind("click", function(){
			$(location).attr('href', $(this).attr('rel'));
		});
	});
	
	$(document).click(function(e){
		var clicked = $(e.target);
		if (!clicked.hasClass("a-dropdown") && dropDownOpen) {
			$(".dropdown").removeClass("active");
		}
	});
	
	if ($("#function-msg-out").length) {
		$("#function-msg-out").delay(2500).slideUp();
	}
});

function placeLoadingImg(divId){
    $(divId).html("<center>"+loadingImg+"</center>");  
}

function hideElement(divId){
	if (divId == null) { return; }
	$("."+divId).each(function(){
		$(this).slideToggle("slow");
	});
}

function hoverToggle(divToHover, divToShow){
	$(divToHover).hover(
		function() {
			$(divToShow).show();
		},
		function() {
			$(divToShow).hide();
		}
	);
}

$(document).bind("ready", function(event) {

	var placeHolder = function(event) {
		$(".placeholder").each(function(i) {
			
			var item = $(this);
			var text = item.attr('rel');
			var form = item.parents('form:first');

			if (item.val() === "" || item.val() === text) {
				item.val(text);
				item.css("color", "#888");
			}
			
			item.bind("focus.placeholder", function(event) {
				if (item.val() === text)
					item.val("");
				item.css('color', "");
			});
			
			item.bind("blur.placeholder", function(event) {
				if (item.val() === ""){
					item.val(text);
					item.css("color", "#888");
				}
			});
			
			form.bind("submit.placeholder", function(event) {
			  if (item.val() === text)
				item.val("");
			});     
			
		});
	}
	placeHolder(event);
});


$(document).ready(function() { 
	remoteService.execute();
});

var tempStatusKey;
var tempStatusText;
var StatusComment = function(key, inputUrl, id, showAllUrl) {
	this.__construct = function(key, inputUrl, id, showAllUrl) {
		$("#comment-input-"+key).click(function() {
			var input = $("#comment_status_activity-"+key).val();
			if (input.length != 0) {
				tempStatusKey = key;
				tempStatusText = input;
				$("#comment-form-"+key).html(loadingImg);
				execute(inputUrl, input, id);
			}
		});
		$("#show-comments-"+key).click(function() {
			$("#show-comments-"+key).html(loadingImg);
			tempStatusKey = key;
			printAllComments(showAllUrl, id);
		});
	}
	var printAllComments = function(url, id) {
		var params = {"id" : id};
		new RemoteAjaxCall(url, params, "StatusCommentShowAllCallBack");
	}
	var execute = function(url, input, id) {
		var params = {"input" : input, "id" : id};
		new RemoteAjaxCall(url, params, "StatusCommentCallBack");
	}
	
	this.__construct(key, inputUrl, id, showAllUrl);
} 

function StatusCommentShowAllCallBack(data) {
	$("#all-comments-status-"+tempStatusKey).html(data);
}
function StatusCommentCallBack(data) {
	if (data.status == 1) {
		$("#new-user-comment-text-"+tempStatusKey).html(data.text);
		$("#new-user-comment-"+tempStatusKey).show();
		$("#comment-form-"+tempStatusKey).remove();
	} else {
		$("#activity-loader").html(data);
	}
}

