var iGeneratorClass = new Class({

	Implements: [Options, Events],

	options: {
		url: {
			send_idea: DIR_WEB_ROOT+'/include/ajax/igeneratorSendIdea.php',
			get_ideas: DIR_WEB_ROOT+'/include/ajax/igeneratorGetIdeas.php'
		},
		classes: {
			vote_plus: 'plus',
			vote_minus: 'minus'
		},
		progress: 'ajax_loading',
		igenerator_area: 'igenerator-area',
		ideas_area: 'ideas-area'
	},

	initialize: function(options){
		this.divShadow=null;
		this.curIdea = null;
	},
	
	showProgress: function(shadow) {
		$(this.options.progress).show();
		if (shadow) {
			this.showShadow();
		}
	},
	
	hideProgress: function() {
		$(this.options.progress).hide();
		this.hideShadow();
	},

	showShadow: function() {
		if (!this.divShadow && $(this.options.photo_area)) {
			this.divShadow=new Element('div',{'class':'shadow'}).set('title','Подождите, идет обработка...');
			this.divShadow.inject(document.body);
			this.divShadow.position({relativeTo: this.options.photo_area, position: 'upperLeft'});
			
			var aSize=$(this.options.photo_area).getSize();
			this.divShadow.setStyle('width',aSize.x);
			this.divShadow.setStyle('height',aSize.y);
			this.divShadow.setStyle('display','block');
		}
	},
	
	hideShadow: function() {
		if (this.divShadow) {
			this.divShadow.destroy();
			this.divShadow=null;
		}
	},
	
	loadIdeas: function(obj, sProblem) {
		
		this.showProgress();
		new Request.JSON({
			url: this.options.url.get_ideas,
			noCache: true,
			data: {sProblem:sProblem},
			onSuccess: function(resp){
				this.hideProgress();
				if (resp.bStateError) {
					msgErrorBox.alert(resp.sMsgTitle,resp.sMsg);
				} else {
					$(this.options.ideas_area).set('html',resp.sText);
					$$('.gen-problem').set('html', resp.sProblem);
				}
			}.bind(this),			
			onFailure: function(){
				this.hideProgress();
				msgErrorBox.alert('Error','Please try again later');
			}
		}).send();
	},

	sendIdea: function(sIdea, sWho, sWhat, sHow, sBetter) {
	
	if(!sIdea)
	{
		alert("Не указана идея!");
		return;
	}
	if(!sWho)
	{
		alert("Не указана целевая аудитория");
		return;
	}
	if(!sWhat)
	{
		alert("Не указана проблема!");
		return;
	}
	if(!sHow)
	{
		alert("Не указан способ решения!");
		return;
	}
	if(!sBetter)
	{
		alert("Не указано преимущество!");
		return;
	}
	
		this.showProgress(1);
		new Request.JSON({
			url: this.options.url.send_idea,
			noCache: true,
			data: {sIdea:sIdea, sWho:sWho, sWhat:sWhat, sHow:sHow, sBetter:sBetter },
			onSuccess: function(resp){
				this.hideProgress();
				if (resp.bStateError) {
					msgErrorBox.alert(resp.sMsgTitle,resp.sMsg);
				} else {
					msgNoticeBox.alert(resp.sMsgTitle,resp.sMsg);
				}
			}.bind(this),			
			onFailure: function(){
				this.hideProgress();
				msgErrorBox.alert('Error','Please try again later');
			}
		}).send();
		return false;
	},
	
	swap: function(obj){
		obj = $(obj);
		if(obj)
		{
			var parent = obj.getParent().getParent();
			var tmp = parent.getChildren('span')[1].get('html');
			var tmp2 = parent.getChildren('span')[3].get('html');
			
			parent.getChildren('span')[1].set('html', tmp2)
			parent.getChildren('span')[3].set('html', tmp)
		}
		return false;
	},
    
	setIdea: function(obj){
		obj = $(obj);
		if(obj)
		{
			$$('.idea-lamps').set('src', DIR_STATIC_SKIN+'/images/lamp0.png');
			var parent = obj.getParent().getParent();
			obj.getChildren('img').set('src', DIR_STATIC_SKIN+'/images/lamp1.png');
			var tmp = parent.getChildren('span')[1].getChildren('a').get('html');
			var tmp2 = parent.getChildren('span')[3].getChildren('a').get('html');
			var idea = tmp+' + '+tmp2;
			$$('.gen-problem').set('html', idea);
			$('igenerator-idea-h').set('value', idea);
			
		}
	},
	
    toggleInfo: function(link, wrapper)
    {
    	link=$(link);
    	var obj=$(wrapper);
    	if(!obj)
    		return;
    	if (Browser.Engine.trident) {
    		obj.toggle();
    	} else {
    		var slideObj = new Fx.Slide(obj);
    		if (obj.getStyle('display')=='none') {
    			slideObj.hide();
    			obj.setStyle('display','block');
    		}
    		slideObj.toggle();
    	}
    	link.toggleClass('open');
    },

	log: function(text, args) {
		if (window.console) console.log(text.substitute(args || {}));
	}
});

window.addEvent('domready', function() {       
      iGenerator=new iGeneratorClass();
});
