var CMS = {Contact: {}, Comments: {}}
var General = {};
var Globals = {}

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

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


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


/* 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("hover");
	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);

/* 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);

