var Mobilly = new Class({
	
	initialize: function() {
		window.addEvent('domready', this.onPageDOMReady.bind(this));
		window.addEvent('load', this.onPageLoad.bind(this));
	},
	
	onPageDOMReady: function() {
      $$('.tipz').each(function(element,index) {
         var content = element.get('title').split('::');
         element.store('tip:title', content[0]);
         element.store('tip:text', content[1]);
      });
      var tipz = new Tips('.tipz',{ className: 'tipz' });
      
      $$('.tipz_blank').each(function(element,index) {
         var content = element.get('title').split('::');
         element.store('tip:title', content[0]);
         element.store('tip:text', content[1]);
      });
      var tipz = new Tips('.tipz_blank',{ className: 'tipz_blank' });

		$$('.cvc_info_img').each(function(element,index) {
         var content = element.get('title').split('::');
         element.store('tip:title', content[0]);
         element.store('tip:text', content[1]);
      });
      var cvc_info_img = new Tips('.cvc_info_img',{ className: 'cvc_info_img' });

      $$('.invitation_info_img').each(function(element,index) {
         var content = element.get('title').split('::');
         element.store('tip:title', content[0]);
         element.store('tip:text', content[1]);
      });
      var invitation_info_img = new Tips('.invitation_info_img',{ className: 'invitation_info_img' });

		this.locationCheck.periodical(500, this);
		
		if($('stickersMouseTarget')) {
			$('stickersMouseTarget').addEvent('mouseenter', function(event) {
				//$('stickersMouseTarget').removeEvents('mouseenter');
				$('stickersMouseRollOver').setStyle('display', 'block')
										  .addEvent('mouseleave', function(event) {
					$('stickersMouseRollOver').setStyle('display', 'none')
											  .removeEvents('mouseleave');
				});
			});
		}
		
		var secSubLinks = $$('.sectionSubAJAX');
		if(secSubLinks.length > 0) {
			secSubLinks.forEach(function(item) {
				var h3 = item.getParent('h3');
				if(!h3) return;
				var container = new Element('div', { 'class': 'sectionSub' }).inject(h3, 'before').grab(h3);
				var content = new Element('div', { 'class' : 'sectionSubContent' }).inject(container);

        new Element('span', { 'class': 'sectionSubLoader' }).inject(h3);
				
				h3.getElement('a').addEvent('click', this.subSectionLoad.bind(this));
			}.bind(this));
		}

      MooTools.lang.set('lv-LV', 'Date', {
         months: ['Janvāris', 'Februāris', 'Marts', 'Aprīlis', 'Maijs', 'Jūnijs', 'Jūlijs', 'Augusts', 'Septembris', 'Oktobris', 'Novembris', 'Decembris'],
         days: ['Svētdiena', 'Pirmdiena', 'Otrdiena', 'Trešdiena', 'Ceturtdiena', 'Piektdiena', 'Sestdiena']
      });
      MooTools.lang.set('ru-RU', 'Date', {
         months: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
         days: ['Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота']
      });
      MooTools.lang.set('en-EN', 'Date', {
         months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
         days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
      });
	},
	
	onPageLoad: function() {
		//startCounting('time-money', 'p', 'tariff', 'p');
		
	},
	
	
	
	locationHash: null,
	locationCheck: function() {
		if(this.locationHash == null || this.locationHash != location.hash) {
			this.locationHash = location.hash;
			if(this.locationHash.substr(1, 1) == 'a') {
				var aId = this.locationHash.substr(2);
				$$('a.sectionSubAJAX').each(function(item) {
					var h = item.get('href').split('=');
					if(h[1] == aId) {
						new Fx.Scroll(window).toElement(item);
						this.subSectionLoad(null, item);
					}
				}.bind(this));
			}
		}
	},
	
	
	
	
	
	subSectionRequest: null,
	subSectionCurElement: null,
	subSectionLoad: function(event, aElement) {
		var h3;
		if(event) {
			event = new Event(event);
			event.preventDefault();
			event.stopPropagation();
			h3 = $(event.target).getParent('h3');
		} else
			h3 = $(aElement).getParent('h3');
		
		if(this.subSectionCurElement == h3.getParent()) {
			this.subSectionCurElement.getElement('.sectionSubContent').get('slide').removeEvents('complete').toggle();
			//console.debug(this.subSectionCurElement.get('isOpen'));
			this.subSectionCurElement.set('isOpen', 1 - this.subSectionCurElement.get('isOpen'));
			//console.debug(this.subSectionCurElement.get('isOpen'));
		} else {
			if(this.subSectionCurElement) {
				this.subSectionUnload(h3.getParent())
			
			} else {
				this.subSectionLoadDo(h3.getParent());
			}
		}
	},
	
	
	subSectionLoadDo: function(nextElement) {
		this.subSectionCurElement = nextElement;
		
		if(this.subSectionRequest) this.subSectionRequest.cancel();
		this.subSectionRequest = new Request.HTML({
			url: this.subSectionCurElement.getElement('.sectionSubAJAX').get('href'),
			//update: this.subSectionCurElement.getElement('.sectionSubContent'),		// this breaks Safari, thus don't use
			evalScripts: true
		});
		this.subSectionRequest.addEvent('complete', this.subSectionLoadComplete.bind(this));
		this.subSectionRequest.get();
	
		this.subSectionCurElement.getElement('.sectionSubLoader').setStyle('visibility', 'visible');	
		
	},
	subSectionLoadComplete: function(responseTree, responseElements, responseHTML, responseJavaScript) {
		if(this.subSectionCurElement) {
			this.subSectionCurElement.getElement('.sectionSubContent').set('html', responseHTML);
			
			this.subSectionCurElement.set('isOpen', 1);
			var h3 = this.subSectionCurElement.getElement('h3');
			h3.getElement('.sectionSubLoader').setStyle('visibility', 'hidden');
			var content = this.subSectionCurElement.getElement('.sectionSubContent');
			
			content.set('slide', {duration: 800, 'link':'cancel', transition: Fx.Transitions.Quint.easeOut}).get('slide').removeEvents('complete');
			content.setStyle('display', 'block').slide('hide').slide('in');
			  
			new Fx.Morph(this.subSectionCurElement, {duration: 200, transition: Fx.Transitions.Sine.easeOut})
				.set({'background-color': '#F18E00'})
				.start({'background-color': '#FFFFFF'});
				
			$('footer').setStyle('display', 'none');
			$('footer').setStyle.delay(800, $('footer'), ['display', 'block']);
		}
		
	},
	
	
	subSectionUnload: function(nextElement) {
		var fxSlide = this.subSectionCurElement.getElement('.sectionSubContent').get('slide');
		//alert(this.subSectionCurElement.get('isOpen'));
		if(this.subSectionCurElement.get('isOpen') > 0) {
			if(this.subSectionCurElement.getPosition().y < nextElement.getPosition().y) {
				new Fx.Scroll(window).toElement($$('.mainBubble')[0]).chain(function() { this.subSectionUnloadDo(fxSlide, nextElement) }.bind(this));
			} else {
				this.subSectionUnloadDo(fxSlide, nextElement);
			}
		} else
			this.subSectionUnloadDo(false, nextElement, true);
	},
	subSectionUnloadDo: function(fxSlide, nextElement, bFast) {
		this.subSectionCurElement.set('isOpen', 0);
		if(fxSlide) {
			if(!bFast) fxSlide.addEvent('complete', this.subSectionLoadDo.bind(this, [nextElement]));
			fxSlide.slideOut();
		}
		
		new Fx.Morph(this.subSectionCurElement, {duration: 800, transition: Fx.Transitions.Sine.easeOut})
			.start({'background-color': '#F18E00'});
			
		if(bFast) this.subSectionLoadDo(nextElement);
	}
	
	
});


var mobilly = new Mobilly();

/*
function startCounting(className, type, tarifClassName, tarifType)
{
	var CarClassParams = new Array(className, type);
	var TarifParams = new Array(tarifClassName, tarifType);
	var cars = new Array();
	var tarif = new Array();

	// skaita laiku
	setInterval("timedCountTime('"+CarClassParams[0]+"', '"+CarClassParams[1]+"')", 1000);

	// skaita tarifu
	cars  = getElementsByClass(CarClassParams[0], CarClassParams[1], -1);
	tarif  = getElementsByClass(TarifParams[0], TarifParams[1], -1);
	var i = 0;
	while (i < cars.length){
		setInterval("timedCountMoney("+i+", '"+CarClassParams[0]+"', '"+CarClassParams[1]+"')", tarif[i].innerHTML);
		i = i+1;
	}
}

function timedCountTime(clas, type)
{
	var time;
	var cars = new Array();
	cars = getElementsByClass(clas, type);

	for (i = 0; i < cars.length; i++)
	{
		dataArr = cars[i].innerHTML.split(" / ");

		time = dataArr[0].split(":");
		time = time.reverse()
		time = addTime(time);
		var seconds = time[0]; var minutes = time[1]; var hours = time[2];
		timeValue = convert(hours) + ':' + convert(minutes) + ':' + convert(seconds);

		cars[i].innerHTML = timeValue + ' / ' + dataArr[1];
	}
}

function timedCountMoney(id, CarClass, CarType)
{
	var car   = getElementsByClass(CarClass, CarType, id);
	dataStr = car.innerHTML + ' / ' + id;  // savieno datus "1:30:50 / 100 / 0";
	dataArr = dataStr.split(" / "); // ievieto masīvā

	var id = dataArr[2];
	var money = dataArr[1].replace(" Ls", "");
	money = money.split(".");
	money = money.reverse()
	money = addMoney(money);

	var santims = money[0];
	if(money[1] >= 0){
		var lats = money[1];
	}else{
		santims = 0;
		var lats = money[0]-1;
	}

	var moneyValue = lats + '.' + convert(santims) + ' Ls';
	car.innerHTML = dataArr[0] + ' / ' + moneyValue;
}

function addTime(time)
{
	var seconds = time[0]*1 + 1;
	var minutes = time[1]*1;
	var hours = time[2]*1;
	if(seconds >= 60){
		minutes += 1;
		seconds = 0;
	}
	if(minutes >= 60){
		hours += 1;
		minutes = 0;
	}
	return new Array(seconds, minutes, hours);
}

function getElementsByClass(searchClass, tag, id) {
	var Elements = new Array();
	var els = document.getElementsByTagName(tag);
	var pattern = new RegExp(searchClass);
	for (i = 0, j = 0; i < els.length; i++) {
		if ( pattern.test(els[i].className) ) {
			Elements[j] = els[i];
			j++;
		}
	}
	if(id >= 0)
	{
		return Elements[id];
	}
	return Elements;
}

function convert(val)
{
	val = val + '';
	if(val.length == 1)
	{
		return '0' + val;
	}
	return val;
}

function addMoney(money)
{
	var santims = money[0]*1 + 1;
	var lats = money[1]*1;
	if(santims >= 100){
		lats += 1;
		santims = 0;
	}
	return new Array(santims, lats);
}
*/