var lang='en';

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){

		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){

		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){

		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){

		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){

		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}


window.addEvent('domready', function(){


	var tabs = new MGFX.Tabs('.tabs','.feature',{
				autoplay: true,
				transitionDuration:500,
				slideInterval:3000,
				hover:true
			});
			
			
	$('convertlang').addEvents({
		'click': function(){
			var en = $('en');
			var mr = $('mr');
			if(lang=='en')
			{
				en.setStyle('display','none'); 
	
				mr.setStyle('opacity','0');
				mr.setStyle('display',''); 
				mr.fade('in');
				this.set('html','Convert this to English');
				lang='mr';
			}
			
			else if(lang=='mr')
			{
				mr.setStyle('display','none'); 
	
				en.setStyle('opacity','0');
				en.setStyle('display',''); 
				en.fade('in');
				this.set('html','Convert this to Marathi');
				lang='en';
			}
			

		}

	});
	
	$$('img.imgbutton').each(function(img) {
		var src = img.getProperty('src');
		var extension = src.substring(src.lastIndexOf('.'),src.length)
		img.addEvent('mouseenter', function() { img.setProperty('src',src.replace(extension,'_hover' + extension)); });
		img.addEvent('mouseleave', function() { img.setProperty('src',src); });
	});
	
		

	//new OverText($('emailtxt'));
	
	$('shufflemore').addEvent('click', function(event) {
		//prevent the page from changing

		event.stop();
		//make the ajax call, replace text
		var req = new Request.HTML({
			method: 'get',
			url: $('shufflemore').get('href'),
			data: { 'do' : '1' },
			noCache: true,
			onRequest: function() { $('shufflink').set('html','Shuffling...<img src="images/ajax-loader.gif"/>'); },
			update: $('shufflink'),
			onComplete: function(response) { //alert('Request completed successfully.');
			}
		}).send();
	});
	
	//new Ticker('TickerVertical',{speed:1000,delay:2000,direction:'vertical'});
	
	$('mailsubsbutton').addEvent('click', function(event) {
		//prevent the page from changing
		event.stop();
		var emailid = $('emailtxt').get('value');
		if(echeck(emailid))
		{
				//make the ajax call, replace text
				var req = new Request.HTML({
					method: 'get',
					url: $('mailsubsbutton').get('href'),
					data: { 'emailid' : emailid },
					noCache: true,
					onRequest: function() { $('emailsubload').set('html','Contacting Sever...<img src="images/email-load.gif"/>'); },
					update: $('subscribedialog'),
					onComplete: function(response) { 
					 $('emailsubload').set('html','');
		TB_show("<b><big>MarathiSS Mailer</big></b>","#TB_inline?height=150&width=400&inlineId=subscribedialog");
					}
				}).send();
		}
		
		else
		alert('Oops! Email id you entered seems to be invaild');
	});
	
	$('hereitisarrow').position({
    relativeTo: $('emailsubs'),
    position: 'upperLeft',
    edge: 'lowerRight',
    offset:{x:30, y: 110}
	});
	

	$('socialarrow').position({
    relativeTo: $('socials'),
       position: 'upperLeft',
    edge: 'lowerRight',
    offset:{x:30, y: 480}
	});
	
	$('showsocials').addEvents({
	
		
	    'mouseover': function(){
	    
	    var sarrow =  $('socialarrow');
	    
	        sarrow.setStyle('display','');
	    },
	    'mouseout': function(){
	     var sarrow =  $('socialarrow');
	     sarrow.setStyle('display','none');
	    }
	});
	
	$('showwhereitis').addEvents({
	
		
	    'mouseover': function(){
	    var arrow =  $('hereitisarrow');
	    
	        arrow.setStyle('display','');
				$('hereitisarrow').position({
    relativeTo: $('emailsubs'),
    position: 'upperLeft',
    edge: 'lowerRight',
    offset:{x:30, y: 20}
	});
	    },
	    'mouseout': function(){
	     var arrow =  $('hereitisarrow');
	     arrow.setStyle('display','none');
	    }
	});

	

	$('feedbackbutton').addEvent('click', function(event) {
		//prevent the page from changing
		//alert('DING!');
		var uemailid = $('uemailid').get('value');
		var uname = $('uname').get('value');
		var ucomments = $('ucomments').get('value');
		//alert('DONG!');
		
		if(echeck(uemailid))
		{
				//make the ajax call, replace text
				var req = new Request.HTML({
					url: 'http://www.marathiss.com/acceptFeedBack.php',
					data: { 'emailid' : uemailid, 'uname' : uname, 'ucomments' : ucomments },
					noCache: true,
					onRequest: function() { $('sendInfo').set('html','Sending...<img src="images/email-load.gif"/>'); },
					update: $('sendInfo'),
					onComplete: function(response) { 
					 $('sendInfo').set('html','');

					}
				}).send();
		}
		
		else
		alert('Oops! Email id you entered seems to be invaild');
	});
	//alert('DIP!');
	
		var up = new FancyUpload2($('demo-status'), $('demo-list'), { // options object
		// we console.log infos, remove that in production!!
		verbose: true,
		
		// url is read from the form, so you just have to change one place
		url: $('form-demo').action,
		
		// path to the SWF file
		path: 'source/Swiff.Uploader.swf',
		
		// remove that line to select all files, or edit it, add more items
		typeFilter: {
			'Images (*.jpg, *.jpeg, *.gif, *.png)': '*.jpg; *.jpeg; *.gif; *.png'
		},
		
		// this is our browse button, *target* is overlayed with the Flash movie
		target: 'demo-browse',
		
		// graceful degradation, onLoad is only called if all went well with Flash
		onLoad: function() {
			$('demo-status').removeClass('hide'); // we show the actual UI
			$('demo-fallback').destroy(); // ... and hide the plain form
			
			// We relay the interactions with the overlayed flash to the link
			this.target.addEvents({
				click: function() {
					return false;
				},
				mouseenter: function() {
					this.addClass('hover');
				},
				mouseleave: function() {
					this.removeClass('hover');
					this.blur();
				},
				mousedown: function() {
					this.focus();
				}
			});

			// Interactions for the 2 other buttons
			
			$('demo-clear').addEvent('click', function() {
				up.remove(); // remove all files
				return false;
			});

			$('demo-upload').addEvent('click', function() {
				up.start(); // start upload
				return false;
			});
		},
		
		// Edit the following lines, it is your custom event handling
		
		/**
		 * Is called when files were not added, "files" is an array of invalid File classes.
		 * 
		 * This example creates a list of error elements directly in the file list, which
		 * hide on click.
		 */ 
		onSelectFail: function(files) {
			files.each(function(file) {
				new Element('li', {
					'class': 'validation-error',
					html: file.validationErrorMessage || file.validationError,
					title: MooTools.lang.get('FancyUpload', 'removeTitle'),
					events: {
						click: function() {
							this.destroy();
						}
					}
				}).inject(this.list, 'top');
			}, this);
		},
		
		/**
		 * This one was directly in FancyUpload2 before, the event makes it
		 * easier for you, to add your own response handling (you probably want
		 * to send something else than JSON or different items).
		 */
		onFileSuccess: function(file, response) {
			var json = new Hash(JSON.decode(response, true) || {});
			
			if (json.get('status') == '0') {
				file.element.addClass('file-failed');
				file.info.set('html', '<strong>An error occured!</strong>'+json.get('error')+' '+json.get('status'));
			
			} 
			else {
				file.element.addClass('file-success');
				file.info.set('html', '<strong>Image was uploaded</strong>');
				}
		},
		
		/**
		 * onFail is called when the Flash movie got bashed by some browser plugin
		 * like Adblock or Flashblock.
		 */
		onFail: function(error) {
			switch (error) {
				case 'hidden': // works after enabling the movie and clicking refresh
					alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
					break;
				case 'blocked': // This no *full* fail, it works after the user clicks the button
					alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
					break;
				case 'empty': // Oh oh, wrong path
					alert('A required file was not found, please be patient and we fix this.');
					break;
				case 'flash': // no flash 9+ :(
					alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
			}
		}
		
	});
	
});
