var CMS = {Contact: {}, Comments: {}, InvitationKit: {}}
var General = {};
var Globals = {}
var Wizkids = {Photos: {}, Contests: {}, Schedule: {}};

$j(document).ready(function(){
  General.domReady();
});

General.domReady = function() {
	this.prepareImages();
}.bind(General);


General.getAbsolutePath = function() {
	return $j("#current-absolute-path").html();
}.bind(General);

/* CMS */
CMS.Contact.save = function(key) {
	var city = (key == "b" ? "bangalore" : "mumbai");
	
	var setResponse = function(msg) {
		$(key + '-response').update(msg);
	};
	var getValue = function(id) {
		return $(key + '-' + id).value;
	};
	
	var params = {
		address: getValue('address'),
		fax: getValue('fax'),
		email: getValue('email'),
		phone: getValue('phone'),
		city: city
	};

	setResponse("Please wait while the information is being saved ...");
	
	var onSuccess = function() {
		setResponse("Successfully updated contact information")
	};
	
	var onFailure = function(val) {setResponse("ERROR: " + val)};
	callJson("/cms/contact/update.php", {parameters: params, onSuccess: onSuccess, onFailure: onFailure, method: 'post'});
}.bind(CMS.Contact);


General.register = function(id) {
	TINY.box.show("/register.php", 1, 400, 330, 2);
}.bind(General);

General.register.submit = function(id) {
	var getValue = function(id) {return $j("#register-" + id).val();};
	var params = {
		name: getValue("name"),
		username: getValue("username"),
		password: getValue("password"),
		email: getValue("email")
	};
	var onFailure = function(t) {
		$j("#register-response").html(t)
	};
	callJson("/register_submit.php", {parameters: params, onSuccess: reload, onFailure: onFailure, method: 'post'});	
}.bind(General.register);

General.postComment = function() {
	var params = {
		body: $j("#new-comment-body").val(),
		page: this.getAbsolutePath()
	};
	var onSuccess = function() {
		alert("Your comment has been posted. It will appear on this page shortly.");
		$j("#new-comment-body").val("");
	};
	callJson("/cms/comments/_submit.php", {parameters: params, onSuccess: onSuccess, method: 'post'});
}.bind(General);

General.viewNewsPost = function(id) {
	TINY.box.show("/news/view.php?id=" + id, 1, 400, 260, 2);
}.bind(General);

General.editNews = function(id) {
	TINY.box.show("/cms/news/edit.php?id=" + id, 1, 400, 260, 2);
}.bind(General);

General.editNews.submit = function() {
	var getValue = function(id) {return $j("#edit-news-" + id).val();};
	var params = {
		body: getValue('body'),
		subject: getValue('subject'),
		id: getValue('id')
	};
	callJson("/cms/news/edit_submit.php", {parameters: params, onSuccess: reload, method: 'post'});
}.bind(General.editNews);

General.deleteNews = function(id) {
	if(!confirm("Are you sure you want to delete this post ?")) return;
	
	callJson("/cms/news/delete.php", {parameters: {id: id}, onSuccess: reload, method: 'post'});
}.bind(General.editNews);

/* Creates the mouse over effect for images */
General.prepareImages = function() {
	var imgs = $j("img[hover]");
	var over = function() {
		if(this.getAttribute('hover')) this.src = this.getAttribute("hover");
	};
	var out = function() {
		if(this.getAttribute('out')) this.src = this.getAttribute("out");
	};
	$A(imgs).each(function(i) {
		i.setAttribute("out", i.src);
	});
	imgs.hover(over, out);
}.bind(General);

General.setActiveMenuItem = function(id) {
	var el = $(id);
	el.src = el.getAttribute("active");
	el.removeAttribute('hover');
	el.removeAttribute('out');
}.bind(General);

/* NETWORK */
General.editNetwork = function(id) {
	TINY.box.show("/cms/network/edit_network.php?id=" + id, 1, 400, 260, 2);
}.bind(General);

General.editNetwork.submit = function() {
	var getValue = function(id) {return $j("#edit-net-" + id).val();};
	var params = {
		city: getValue('city'),
		address: getValue('address'),
		position: getValue('position'),
		id: getValue('id')
	};
	callJson("/cms/network/edit_network_submit.php", {parameters: params, onSuccess: reload, method: 'post'});
}.bind(General.editNetwork);

General.deleteNetwork = function(id) {
	if(!confirm("Are you sure you want to delete this network ?")) return;
	
	callJson("/cms/network/delete_network_submit.php", {parameters: {id: id}, onSuccess: reload, method: 'post'});
}.bind(General.editNetwork);

General.handleNetworkHovers = function() {
	var items = $j("[hover=shownetwork]");
	
	var constructPopup = function(el) {
		var arr = el.getAttribute("position").split(",");
		var top = (parseInt(arr[1]) - 160); var left = (parseInt(arr[0]) - 15);
		var c = new Element('div', {style: "position: absolute; left: "+ left +"px; top: "+ top +"px", 'class': "network-address-popup"});
		var d = new Element('div', {'class': 'header'}).update("city")
		var e = new Element('div', {'class': 'text'}).update("text is here");
		var img = new Element('img', {src: "/images/map_talkbox_bottom.png"});
		$(c).appendChild(d);
		$(c).appendChild(e);
		$('map-container').appendChild(c);
		d.update(el.findDescendantsByClassName('city')[0].innerHTML);
		e.update(el.findDescendantsByClassName('info')[0].innerHTML);
		$(e).appendChild(img); // Adds the image to the bottom
	}.bind(this);
	var over = function() {
		constructPopup(this);
		this.addClassName("bold");
	};
	var out = function() {
		$j(".network-address-popup").fadeOut();
		this.removeClassName("bold");
	};
	items.hover(over, out)
}.bind(General);

General.networkShowPointerPosition = function(e) {
	if(!Globals.networkIndiaMap) 
		Globals.networkIndiaMap = $('india-map');
	var map = Globals.networkIndiaMap;
	var mapPos = map.cumulativeOffset();
	var left = e.pointerX() - mapPos[0];
	var top = e.pointerY() - mapPos[1];
	
	$j("#pointer-position").css({visibility: "visible", left: left, top: (top - 30)}).html("Pointer Position: " + left + "," + top);

}.bind(General);

General.networkCreateIndiaMapEvent = function() {
	$('india-map').observe('mousemove', this.networkShowPointerPosition);
}.bind(General);

General.facebookConnect = function() {
	window.open("/facebook/index.php",'Facebook-Connect','height=200,width=450');
	TINY.box.show("/facebook/reload_popup.php", 1, 400, 80, 2);
}.bind(General);

/* CMS COMMENTS */
CMS.Comments.edit = function(id) {
	TINY.box.show("/cms/comments/_edit.php?id=" + id, 1, 400, 180, 2);
}.bind(CMS.Comments);

CMS.Comments.edit.submit = function() {
	var params = {
		body: $j("#edit-comment-body").val(),
		id: $j("#edit-comment-id").val()
	};
	callJson("/cms/comments/_edit_submit.php", {parameters: params, onSuccess: reload, method: 'post'});
}.bind(CMS.Comments.edit);

CMS.Comments.publish = function(id) {
	var params = {
		id: id
	};
	callJson("/cms/comments/_publish.php", {parameters: params, onSuccess: reload, method: 'post'});
}.bind(CMS.Comments);

CMS.Comments.unpublish = function(id) {
	var params = {id: id};
	callJson("/cms/comments/_unpublish.php", {parameters: params, onSuccess: reload, method: 'post'});
}.bind(CMS.Comments);

CMS.Comments.delete = function(id) {
	var params = {id: id};
	callJson("/cms/comments/_delete.php", {parameters: params, onSuccess: reload, method: 'post'});
}.bind(CMS.Comments);

CMS.InvitationKit.remove = function(id) {
	if(!confirm("Are you sure you want to delete this item?")) return;
	var url = "/cms/wizkids/delete_invitation_kit_submit.php?id=" + id;
	loadUrl(url);
}.bind(CMS.InvitationKit);

/* WIZKIDS */
Wizkids.Photos.next = function() {
	var ac = $j(".photos-container [pindex]");
	var i = ac[0].getAttribute("pindex");
	i++;
	this.set(i);
}.bind(Wizkids.Photos);

Wizkids.Photos.back  = function() {
	var ac = $j(".photos-container [pindex]");
	var i = ac[0].getAttribute("pindex");
	i--;
	this.set(i);
}.bind(Wizkids.Photos);

Wizkids.Photos.set = function(i) {
	var ac = $j(".photos-container [pindex]");
	
	var anchor = $j("[photoanchor=true]");
	anchor.remove();
	var nextPic = $j(".photo[pindex="+ i +"] IMG");
	if(nextPic.length){
		var src = nextPic[0].getAttribute("origsrc");
		var loadingText = new Element('div', {style: "padding: 10px", "name": "loading-text"}).update("Loading Image ...");
		ac[0].appendChild(loadingText);
		
		var img = new Element('img', {src: src, photoanchor: "true", style: "max-height: 360px; max-width: 550px", onload: "Wizkids.Photos.positionImage()"});
		$j(img).css({opacity: 0});
		ac[0].appendChild(img);
		ac[0].setAttribute("pindex", i);
	}
	
	var showNext = $j(".photo[pindex="+ (i+1) +"]").length;
	if(!showNext)
		$j(".arrow.right").addClass("disabled");
	else
		$j(".arrow.right").removeClass("disabled");
		
	if(i == 1)
		$j(".arrow.left").addClass("disabled");
	else
		$j(".arrow.left").removeClass("disabled");
		
	$j(".photo").removeClass("selected");
	$j(".photo[pindex="+ i +"]").addClass("selected");
		
}.bind(Wizkids.Photos);

Wizkids.Photos.positionImage = function() {
	var jimg = $j("td IMG").last();
	
	// Check added since sometimes the image does not get its height property in time.
	if(jimg.height() == 0 || jimg.width() == 0){
		window.setTimeout(this.positionImage, 400);
		return;
	}
	$j("[name=loading-text]").remove();
	var par = jimg.parent();
	var left = par.width() > jimg.width() ? (par.width() - jimg.width()) / 2 : 0;
	var top = par.height() > jimg[0].getHeight() ? (par.height() - jimg[0].getHeight()) / 2 : 0;
	jimg.css({position: "absolute", left: left, top: top}).animate({opacity: 1});
}.bind(Wizkids.Photos);

Wizkids.Contests.showDesc = function(id) {
	var data = $j("#contest-" + id).html();
	var el = $j("#contest-description").html(data);
	var height = $(el[0]).getContentHeight();
	el.animate({height: height});
}.bind(Wizkids.Contents);

Wizkids.Schedule.showContactInfo = function(el) {
	el = $j(el).parent();
	var info = $j("[name=contact-info]", $j(el));
	$j('#global-contact-info').html(info.html());
}.bind(Wizkids.Schedule);

Wizkids.showPageInModal = function(url) {
	TINY.box.show({url: url, boxid: "page", close: false})
}.bind(Wizkids);
