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']
      });


            var selectmenu=document.getElementById("account_select");
            if (selectmenu != null){
               selectmenu.onchange=function(){
                  var chosenoption=this.options[this.selectedIndex];
                  if (chosenoption.value!="nothing"){
                     if(document.location.href.substr(-1) == "/"){
                        window.location = document.location.href + "?account=" + chosenoption.value;
                     }else{
                        window.location = document.location.href + "&account=" + chosenoption.value;
                     }
                  }
               }
            }
	},
	
	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();
