// JavaScript Document

Element.implement({ 
	hasEvent: function(eventType,fn) { 
		//get the element's events 
		var myEvents = this.retrieve('events'); 
		//can we shoot this down? 
		return myEvents && myEvents[eventType] && (fn == undefined || myEvents[eventType].keys.contains(fn)); 
	}
});

window.addEvent('domready', function(){
	setupImageClicks();
});


function setupImageClicks(){
	$$('.category img.clickable').each(function(img){
		if(img.get('src') == 'app/images/expand-v.gif'){
			img.getParent().getElements('.children')[0].loaded = true;
		}
		if(!img.hasEvent('click')){
			img.addEvent('click', processImgClick_Category);
		}
	});
	
	$$('.countryzone img.clickable').each(function(img){
		if(img.get('src') == 'app/images/expand-v.gif'){
			img.getParent().getElements('.children')[0].loaded = true;
		}
		if(!img.hasEvent('click')){
			img.addEvent('click', processImgClick_CountryZone);
		}
	});
	
	$$('.category input').each(function(input){
		if(input.get('type') == 'checkbox'){
			if(!input.hasEvent('click')){
				input.addEvent('click', processCbxClick_Category);
			}
		}
	});
	
	$$('.countryzone input').each(function(input){
		if(input.get('type') == 'checkbox'){
			if(!input.hasEvent('click')){
				input.addEvent('click', processCbxClick_CountryZone);
			}
		}
	});
}


function processImgClick_Category(){
	var categoryid = this.get('id').substring(4);

	if(this.get('src') == 'app/images/expand-h.gif'){
		this.set('src', 'app/images/expand-v.gif');
		if(this.getParent().getElements('.children')[0].loaded == null){
			this.getParent().set('tween', {duration: '700', link: 'chain'});
			this.getParent().setStyle('background-color', '#ffF');
			this.getParent().tween('background-color', '#ffc');
			var AJAXurl = '?circuit=ajax&fuseaction=categories&categoryid='+categoryid+'&'+sesstoken;
			var req = new Request.HTML({
				url: AJAXurl,
				onSuccess: function(a, b, html){
					$('cat_'+this.fetchingId).getElements('.children')[0].set('html', html);
					$('cat_'+this.fetchingId).getElements('.children')[0].loaded = true;
					$('cat_'+this.fetchingId).tween('background-color', '#fff');
					setupImageClicks();
				}
			});
			req.fetchingId = categoryid;
			req.send();
		}
		else{
			this.getParent().getElements('.children')[0].setStyle('display', '');
		}
	}
	else{
		this.set('src', 'app/images/expand-h.gif');
		this.getParent().getElements('.children')[0].setStyle('display', 'none');
	}
}

function processCbxClick_Category(){
	var categoryid = this.get('id').substring(4);

	if(this.get('checked')){
		this.getParent().getElements('img.clickable')[0].set('src', 'app/images/expand-v.gif');
		if(this.getParent().getElements('.children')[0].loaded == null){
			this.getParent().set('tween', {duration: '700', link: 'chain'});
			this.getParent().setStyle('background-color', '#ffF');
			this.getParent().tween('background-color', '#ffc');
			var AJAXurl = '?circuit=ajax&fuseaction=categories&categoryid='+categoryid+'&'+sesstoken;
			var req = new Request.HTML({
				url: AJAXurl,
				onSuccess: function(a, b, html){
					$('cat_'+this.fetchingId).getElements('.children')[0].set('html', html);
					$('cat_'+this.fetchingId).getElements('.children')[0].loaded = true;
					$('cat_'+this.fetchingId).tween('background-color', '#fff');
					setupImageClicks();
				}
			});
			req.fetchingId = categoryid;
			req.send();
		}
		else{
			this.getParent().getElements('.children')[0].setStyle('display', '');
		}
	}
	else{
		this.getParent().getElements('img.clickable')[0].set('src', 'app/images/expand-h.gif');
		this.getParent().getElements('.children')[0].setStyle('display', 'none');
	}
}

function processImgClick_CountryZone(){
	var countryzoneid = this.get('id').substring(4);

	if(this.get('src') == 'app/images/expand-h.gif'){
		this.set('src', 'app/images/expand-v.gif');
		if(this.getParent().getElements('.children')[0].loaded == null){
			this.getParent().set('tween', {duration: '700', link: 'chain'});
			this.getParent().setStyle('background-color', '#ffF');
			this.getParent().tween('background-color', '#ffc');
			var AJAXurl = '?circuit=ajax&fuseaction=countries&countryid='+countryzoneid+'&'+sesstoken;
			var req = new Request.HTML({
				url: AJAXurl,
				onSuccess: function(a, b, html){
					$('zone_'+this.fetchingId).getElements('.children')[0].set('html', html);
					$('zone_'+this.fetchingId).getElements('.children')[0].loaded = true;
					$('zone_'+this.fetchingId).tween('background-color', '#fff');
					setupImageClicks();
				}
			});
			req.fetchingId = countryzoneid;
			req.send();
		}
		else{
			this.getParent().getElements('.children')[0].setStyle('display', '');
		}
	}
	else{
		this.set('src', 'app/images/expand-h.gif');
		this.getParent().getElements('.children')[0].setStyle('display', 'none');
	}
}


function processCbxClick_CountryZone(){
	var countryzoneid = this.get('id').substring(4);

	if(this.get('checked')){
		this.getParent().getElements('img.clickable')[0].set('src', 'app/images/expand-v.gif');
		if(this.getParent().getElements('.children')[0].loaded == null){
			this.getParent().set('tween', {duration: '700', link: 'chain'});
			this.getParent().setStyle('background-color', '#ffF');
			this.getParent().tween('background-color', '#ffc');
			var AJAXurl = '?circuit=ajax&fuseaction=countries&countryid='+countryzoneid+'&'+sesstoken;
			var req = new Request.HTML({
				url: AJAXurl,
				onSuccess: function(a, b, html){
					$('zone_'+this.fetchingId).getElements('.children')[0].set('html', html);
					$('zone_'+this.fetchingId).getElements('.children')[0].loaded = true;
					$('zone_'+this.fetchingId).tween('background-color', '#fff');
					setupImageClicks();
				}
			});
			req.fetchingId = countryzoneid;
			req.send();
		}
		else{
			this.getParent().getElements('.children')[0].setStyle('display', '');
		}
	}
	else{
		this.getParent().getElements('img.clickable')[0].set('src', 'app/images/expand-h.gif');
		this.getParent().getElements('.children')[0].setStyle('display', 'none');
	}
}

