var CMS = {Contact: {}, Comments: {}}
var General = {TopMenu: {ddRelease: 0}, TinyMCE: {}};
var Globals = {}
var AScroller = {moveBy: 100};

$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) {
	aaa = 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.TopMenu.showAbout = function() {
	this.ddRelease++;
	$j(".about .options").show();
}.bind(General.TopMenu);

General.TopMenu.hideAbout = function() {
	this.ddRelease--;
	var fn = function(){
		if(this.ddRelease > 0) return;
		$j(".about .options").hide();
	}.bind(this)
	window.setTimeout(fn, 1000);
}.bind(General.TopMenu);

General.TopMenu.showYears = function() {
	$j(".years").show();
}.bind(General.TopMenu);

General.TopMenu.hideYears = function() {
//	var fn = function() {
		$j(".years").hide();	
//	};
//	window.setTimeout(fn, 400)
}.bind(General.TopMenu);

General.TopMenu.showMonths = function(el) {
	$j(".months", $j(el)).show();
}.bind(General.TopMenu);

General.TopMenu.hideMonths = function(el) {
	$j(".months", $j(el)).hide();	
}.bind(General.TopMenu);

General.TinyMCE.init = function() {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_styles: "",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,formatselect,fontsizeselect",
		theme_advanced_buttons2: "",
		theme_advanced_buttons3: ""
	});
}.bind(General.TinyMCE);

/* ARTICLE SCROLLER */
AScroller.left = function() {
	var el = $j("[articlescontainer=true]");
	var moveBy = -(this.currentMarginLeft()) < this.moveBy ? 0 : (this.currentMarginLeft() + this.moveBy);
	el.animate({marginLeft: moveBy});
}.bind(AScroller);

AScroller.right = function() {
	var el = $j("[articlescontainer=true]");
	if(!el[0].getAttribute('maxscroll'))
		this.setMaxScroll();
	var moveBy = -(this.currentMarginLeft()) > (parseInt(el[0].getAttribute('maxscroll') - this.moveBy)) ? this.currentMarginLeft() : (this.currentMarginLeft() - this.moveBy);
	el.animate({marginLeft: moveBy})
}.bind(AScroller);

AScroller.setMaxScroll = function() {
	var el = $j("[articlescontainer=true]");
	el[0].setAttribute("maxscroll", el.width());
}.bind(AScroller);

AScroller.currentMarginLeft = function() {
	var el = $j("[articlescontainer=true]");
	return parseInt(el.css('marginLeft').replace('px'));
}.bind(AScroller);
