/*************/
function array_merge(one, two) {
  one.push(two);
  return one.flatten();
}

function isdefined(variable)
{
    return (typeof(variable) == "undefined") ? false: true;
}

function hide_opacity(el){
	if(el) {
		YAHOO.util.Dom.setStyle(el, "opacity", "0");
		//if(YAHOO.util.Dom.getStyle(el, "opacity") == '1') el.setStyle("opacity", "0");
	}
}

function fadeInOpacity(id, val, time){
	hide_opacity($(id));
	if($(id).getStyle('display') == 'none') show($(id));
	var fadeInAnim = new YAHOO.util.Anim(($(id)), { opacity: {to: val} }, time );
	fadeInAnim.onComplete.subscribe(function() { });
	fadeInAnim.animate();
}

function show(el){
	if(el) el.setStyle({display: ''});
}

function hide(el){
	if(el) el.setStyle({display: 'none'});
}

function checkDate(o) {
	var filter = /^([0-9]{2})\-([0-9]{2})\-([0-9]{4})$/;
	return (!filter.test(o) ? false : true);
}

function checkEmail(o) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return (!filter.test(o.value) ? false : true);
}

function toogle_check(id){
	if($(id)) $(id).checked = $(id).checked == true ? false : true;
}

function toogle_radio(id){
	if($(id) && !$(id).checked) $(id).checked = true;
}

function showHide(id){
	if($(id))
		 if($(id).getStyle('display') == 'none')
			$(id).setStyle({display: ''});
		 else $(id).setStyle({display: 'none'});
}

function setActiveNav(active){
	var nav = ['navig_epastamarketings','navig_mes','navig_raksti','navig_home','navig_features','navig_partnership','navig_contact'], i;
	for(i in nav){
		if($(nav[i])) $(nav[i]).className = '';
	}
	if($(active)) $(active).className = 'active';
}

function checkRegister(lang, msg){
	if($('name').value == '') alert(msg.no_name);
	else if(!$F('surname')) alert(msg.no_surname);
	else if(!$F('company')) alert(msg.no_company);
	else if(!$F('email')) alert(msg.no_email);
	else if(!$F('pass1')) alert(msg.no_pass1);
	else if(!$F('pass2')) alert(msg.no_pass_2);
	else if($F('pass1') != $F('pass2')) alert(msg.pass_notequal);
	else if(!(/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/.test($('email').value))) alert(msg.bad_email);
	else if($F('pass1').length < 6 || $F('pass2').length < 6) alert(msg.pass_short);
	else {
		var params = 'name='+$F('name')+'&surname='+$F('surname')+'&company='+$F('company');
		params += '&email='+$F('email')+'&pass='+$F('pass1');
		params += '&lang='+($('lang_lv') && $('lang_lv').checked == true ? 1 : 3)+'&plan='+$F('plan');
		$('continue').disabled = true;

		new Ajax.Request('/'+lang+'/register/', {
			asynchronous: true,
			method: 'post',
			parameters: params,
			requestHeaders: {Accept: 'application/json'},
			onSuccess: function(resp){
				var json = resp.responseText.evalJSON(true);
				if(json.error){
					alert(json.error_descr);
					$('continue').disabled = false;
				}
				else Lightview.show({href: '/'+lang+'/register/confirm/',rel: 'ajax',options: {autosize: true,topclose: true,ajax: {onComplete: function(resp){}}}});
			}
		});
	}
	return false;
}

/****************   SLIDER   ****************/
/*
 *
 * Slideshow script 1.2
 * Created on 16:02 19.09.2010 by Romans Ruskovs
 *
	Example:
 	var slideshow = new Slideshow(6, 0.6, 'slider_slides', 960);
	slideshow.setSideSwitcher('switcher_left', 'left');
	slideshow.setSideSwitcher('switcher_right', 'right');
	var cnt = $('numeric_switchers').childElements().size();
	for (i = 1; i <= cnt; i++) slideshow.setNumericSwitcher(i, cnt, 'switcher_');
	slideshow.start();
 *
 */

var Slideshow = Class.create({
	 initialize: function(slideShowDelay, slideChangeDelay, parentName, parentWidth) {
		this.delay_show = slideShowDelay;
		this.delay_change = slideChangeDelay;
		this.parentName = parentName;
		this.parentWidth = parentWidth;
		this.curElmtNum = 1;
		this.oldMarginLeft = 0;
		this.newMarginLeft = 0;
		this.numericSwitchers = [];
		this.slideShowDrctn = 'right';
		this.totalElmt = $(parentName).childElements().size();

		$(this.parentName).observe('mouseover', function(s){
			this.setPaused(0);
		}.bind(this));

		$(this.parentName).observe('mouseout', function(s){
			this.setUnPaused(0);
		}.bind(this));

		this.makeTmp();
	 },

	mran: function mran(ma,mi){
		return(Math.round(Math.random()*(ma-mi))+mi)
	 },

	//element for tmp values
	makeTmp: function(){
		var rnd =  this.mran(999, 1);
		var a = new Element('input', { 'type': 'hidden', 'id': 'paused_' + rnd, 'value': 0 });
		$(this.parentName).appendChild(a);
		this.paused_id = 'paused_' + rnd;
		this.paused_el = a;

		var a = new Element('input', { 'type': 'hidden', 'id': 'mouseover_' + rnd, 'value': 0 });
		$(this.parentName).appendChild(a);
		this.mouseover_id = 'mouseover_' + rnd;
		this.mouseover_el = a;
	 },

	setPaused: function(over){
		this.paused_el.value = 1;
		this.mouseover_el.value = over;
	 },

	setUnPaused: function(over){
		this.paused_el.value = 0;
		this.mouseover_el.value = over;
	 },

	getPaused: function(){
		return $(this.paused_id).value;
	 },

	changeSlide: function(is_show, over) {
		this.checkSlide(is_show);
		var parentName = this.parentName;
		var delay_change = this.delay_change;
		var paused_id = this.paused_id;
		var mouseover_id = this.mouseover_id;

		this.setPaused(over);

		new Effect.Tween(parentName, this.oldMarginLeft, this.newMarginLeft,
			{
				duration: delay_change,
				afterFinish: function(){
					if($(mouseover_id).value == 0) $(paused_id).value = 0;
				}
			},
			function(p) {
				$(parentName).setStyle({
					marginLeft: p+'px'
				});
			}
		);

		this.highlightNumericSwitch(this.curElmtNum);
		this.numericSwitchOnChange(this.curElmtNum);
	 },

	checkSlide: function(is_show) {
		this.oldMarginLeft = this.newMarginLeft;

		//set direction and next slide number
		if(is_show){
			if(this.slideShowDrctn == 'left'){
				this.curElmtNum--;
				if (!this.curElmtNum) {
					this.curElmtNum = 2;
					this.slideShowDrctn = 'right';
				}
			}
			else{
				this.curElmtNum++;
				if (this.curElmtNum > this.totalElmt) {
					this.curElmtNum = this.totalElmt-1;
					this.slideShowDrctn = 'left';
				}
			}
		}
		this.newMarginLeft = (-1)*(this.parentWidth * (this.curElmtNum - 1));
	},

	start: function() {
		this.executor = new PeriodicalExecuter(function() {
			if (this.getPaused() == '0') this.changeSlide(1, 0);
		}.bind(this), this.delay_show);
	 },

	setSideSwitcher: function(o_id, drctn){
		$(o_id).observe('click', function(s){
			if ((drctn == 'left' && this.curElmtNum != 1) ||
				(drctn == 'right' && this.curElmtNum < this.totalElmt)) {

				if(drctn == 'left') this.curElmtNum--;
				else this.curElmtNum++;

				this.changeSlide(0, 1);
			}
		}.bind(this));

		var paused_id = this.paused_id;
		var mouseover_id = this.mouseover_id;

		$(o_id).observe('mouseover', function(s){
			$(paused_id).value = 1;
			$(mouseover_id).value = 1;
		}.bind(this));

		$(o_id).observe('mouseout', function(s){
			$(paused_id).value = 0;
			$(mouseover_id).value = 0;
		}.bind(this));
	 },

	setNumericSwitcher: function(toSlide, totalSlides, tpl){
		this.numericSwitchers[toSlide] = tpl + toSlide;

		$(tpl + toSlide).observe('click', function(s){
			//set switcher active
			this.highlightNumericSwitch(toSlide);
			//set new slide
			this.curElmtNum = toSlide;
			this.changeSlide(0, 1);
		}.bind(this));

		var paused_id = this.paused_id;
		var mouseover_id = this.mouseover_id;

		$(tpl + toSlide).observe('mouseover', function(){
			$(paused_id).value = 1;
			$(mouseover_id).value = 1;
		}.bind(this));

		$(tpl + toSlide).observe('mouseout', function(){
			$(paused_id).value = 0;
			$(mouseover_id).value = 0;
		}.bind(this));
	},

	setNumericSwitcherOnChange: function(fnc){
		this.numericSwitchOnChange = fnc;
	 },

	numericSwitchOnChange: function(slideNum){
		//
	 },

	highlightNumericSwitch: function(o_id){
		if(isdefined(this.numericSwitchers[o_id])){
			//remove active switcher
			this.numericSwitchers.each(function(el){
				if(isdefined($(el))) $(el).className = '';
			});

			//set new active switcher
			$(this.numericSwitchers[o_id]).className = 'active';
		}
	 }
});
/****************   SLIDER e  ****************/
/* consult form */
var Form_Consult = Class.create(Slideshow, {
  	 initialize: function(slideDelay, fadeDelay, parentName, parentHeight) {
		this.slideDelay = slideDelay;
		this.fadeDelay = fadeDelay;
		this.parentName = parentName;
		this.parentHeight = parentHeight;
		this.oldMarginTop = parentHeight;
		this.newMarginTop = 0;
		this.fadeFrom = 0;
		this.fadeTo = 1;
		this.formFields = new Hash();

		$(parentName).setStyle({
			'opacity': this.fadeFrom
		});

		this.makeTmp();
	 },

	addToggleButton: function(btnID){
		$(btnID).observe('click', function(s){
			if(this.getPaused() == 0) this.toggleForm();
		}.bind(this));
	 },

	toggleForm: function(){
		var parentName = this.parentName;
		var slideDelay = this.slideDelay;
		var fadeDelay = this.fadeDelay;
		var paused_id = this.paused_id;

		var newMarginTop = this.newMarginTop;
		var oldMarginTop = this.oldMarginTop;

		var fadeFrom = this.fadeFrom;
		var fadeTo = this.fadeTo;

		this.newMarginTop = this.newMarginTop ? 0 : this.parentHeight;
		this.oldMarginTop = this.oldMarginTop ? 0 : this.parentHeight;

		this.fadeFrom = this.fadeFrom ? 0 : 1;
		this.fadeTo = this.fadeTo ? 0 : 1;

		this.setPaused(0);

		new Effect.Tween(parentName, oldMarginTop, newMarginTop,
			{
				duration: slideDelay,
				afterFinish: function(){
					$(paused_id).value = 0;
				}
			},
			function(p) {
				$(parentName).setStyle({
					marginTop: p+'px'
				});
			}
		);

		new Effect.Opacity(parentName,
			{
				duration: fadeDelay,
				transition: Effect.Transitions.linear,
				from: fadeFrom,
				to: fadeTo
			}
		);
	 },

	addFormFields: function(fldID, msg){
		this.formFields.set(fldID, msg);
	 },

	addSubmitButton: function(btnID, msg){
		$(btnID).observe('click', function(s){			
			this.submitForm(msg);
		}.bind(this));
	 },

	checkFields: function(){
		var er = false;
		this.formFields.each(function(arr){			
			if(!er && $(arr[0]).value == '') {
				alert(arr[1]);
				$(arr[0]).focus();
				er = true;
			}
		});
		return !er;
	 },

	submitForm: function(msg){
		if(!this.checkFields()) return false;

		var params = '', arr = [], i=0;
		this.formFields.each(function(el){			
			arr[i] = el[0]+'='+$F(el[0]);
			i++;
		});
		params = arr.join('&');

		this.toggleForm();
		
		new Ajax.Request('/getconsultation', {
			asynchronous: true,
			method: 'post',
			parameters: params,
			onSuccess: function(resp){				
				alert(msg);
			}
		});

	}
});
/* consult form e*/
