

var root_path = './';

var abs_root = 'http://ectv.ec-ftu.org/';

var fcomment_url = 'engine/ajax/ajax_comment.php';
var fcpage_url = 'engine/ajax/ajax_commentpage.php';
var fvcomment_url = 'engine/ajax/ajax_comment_video.php';
var fvcpage_url = 'engine/ajax/ajax_commentpage_video.php';
var fcal_url = 'engine/ajax/ajax_calendar.php';
var fpoll_url = 'engine/ajax/ajax_poll.php';
var fvidectv_url = 'engine/ajax/ajax_ectv.php';
var flook_url = 'engine/ajax/look.php';

// Header Task
function taskover() {	
	$$('img.headertask').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('_def','')); });
		img.addEvent('mouseleave', function() { img.setProperty('src',src); });
	});
}

// Login
function login() {
	Shadowbox.open({
						content: $('loginbox').get('html'),
						player:  "html",
						height: 225,
						width: 390						
	});
}

// Study Page

function createPlayer(vid) {
            var s1 = new SWFObject('http://ia311324.us.archive.org/1/items/Zchannel/player.swf','ply','304','228','9','#ffffff');
  				s1.addParam('allowfullscreen','true');
  				s1.addParam('allowscriptaccess','always');
  				s1.addParam('wmode','transparent');			
  				s1.addParam('flashvars','image=http://www.ec-ftu.org/images/ecchannel/zchannel.gif&file=' + vid +'&skin=http://ia311324.us.archive.org/1/items/Zchannel/bekle.swf&frontcolor=ffffff&lightcolor=cc9900&controlbar=over');				
  				s1.write('mediaspace');
}

// Comment Pagination

function paginate(curpage,numpage) {
	var cpage_load = $('cpage_loader');
	var com_container = $('comment_reponse');	
	//make the ajax call
		var req = new Request.JSON({
			url: root_path + fcpage_url,			
			onComplete: function(response) {		
				if (response.is_ok) {					
					com_container.set('html','');
					com_container.set('html',response.c_mess);					
				} else {
					alert('Failed...');
				}
			}
		}).get({'cpage' : curpage, 'npage' : numpage, 'n' : $('newsid').get('value')});
}

function vpaginate(curpage,numpage) {
	var cpage_load = $('cpage_loader');
	var com_container = $('comment_reponse');	
	//make the ajax call
		var req = new Request.JSON({
			url: abs_root + fvcpage_url,			
			onComplete: function(response) {		
				if (response.is_ok) {					
					com_container.set('html','');
					com_container.set('html',response.c_mess);					
				} else {
					alert('Failed...');
				}
			}
		}).get({'cpage' : curpage, 'npage' : numpage, 'n' : $('video_id').get('value')});
}

// Add a comment

function scomment() {

	var com_button = $('postcom');
	var com_form = $('comment');
	var com_load = $('comment_loader');
	var com_container = $('comment_reponse');

	if (com_button) {
		com_button.addEvent('click', function(e) {
		new Event(e).stop();
		this.disabled = 1;
		this.addClass('disabled');
		com_load.addClass('loading');
		//make the ajax call
		var req = new Request.JSON({
			url: root_path + fcomment_url,			
			onComplete: function(response) {
				com_button.disabled = 0;
				com_button.removeClass('disabled');
				com_load.removeClass('loading');
				if (response.is_ok) {					
					com_container.set('html','');
					com_container.set('html',response.c_mess);
					tinyMCE.get('comments').setContent('')
				} else {
					alert(response.e_mess);
				}
			}
		}).post({'q' : tinyMCE.get('comments').getContent(), 'n' : $('newsid').get('value'), 'cstart' : $('curpage').get('value')});
	})
	}
}

// Add a comment

function svcomment() {

	var com_button = $('postcom');
	var com_form = $('comment');
	var com_load = $('comment_loader');
	var com_container = $('comment_reponse');

	if (com_button) {
		com_button.addEvent('click', function(e) {
		new Event(e).stop();
		this.disabled = 1;
		this.addClass('disabled');
		com_load.addClass('loading');
		//make the ajax call
		var req = new Request.JSON({
			url: abs_root + fvcomment_url,			
			onComplete: function(response) {
				com_button.disabled = 0;
				com_button.removeClass('disabled');
				com_load.removeClass('loading');
				if (response.is_ok) {					
					com_container.set('html','');
					com_container.set('html',response.c_mess);
					tinyMCE.get('comments').setContent('')
				} else {
					alert(response.e_mess);
				}
			}
		}).post({'q' : tinyMCE.get('comments').getContent(), 'n' : $('video_id').get('value'), 'cstart' : $('curpage').get('value')});
	})
	}
}

// Calendar

function doCal(act) {

	var cal_month = $('month');
	var cal_year = $('year');
	var cal_div = $('eventcalendar');
	var cal_container = $('calendar');
		
	cal_div.addClass('loading');

		//make the ajax call
		var req = new Request.JSON({
			url: root_path + fcal_url,
			onComplete: function(response) {
				cal_div.removeClass('loading');
				cal_container.set('html','');
				cal_container.set('html',response.html);
				// Day tooltip				
				jQuery('.daylink').each(function() {
					var content = jQuery(this).next().html();			
					jQuery(this).bt(content, {
					positions: ['right', 'top']
					});	
				});
			}	
		}).post({'act' : act , 'month' : cal_month.get('value'), 'year' : cal_year.get('value')});

}

// Poll

function animateResults(poll_id,w){
  jQuery("#poll_result" + poll_id + " img").each(function(){
      var percentage = jQuery(this).next().text();
      jQuery(this).css({width: "0%"}).animate({
				width: w * percentage}, 'slow');
  });
}

function doPoll(act,poll_id,w) {

	var poll_div = $('poll_result' + poll_id);
	var poll_container = $('poll_container' + poll_id);
	var vote = $$('#poll_container' + poll_id + ' input[name=vote_check]:checked').get('value');
	var params = '';

	poll_container.setStyle('background-image','url("./images/ajax-loader.gif")');
	jQuery("#poll_result" + poll_id).fadeOut("slow");

	$$('.pollbut').disabled = 1;	

		//make the ajax call
		if (Cookie.read('poll' + poll_id) || act == 'results') {
				param = '?poll_id=' + poll_id;
			} else if (act == 'vote') {
				Cookie.write('poll' + poll_id, 1, {expires: 7});
				param = '?poll_id=' + poll_id + '&vote=' + vote; 				
			}

	var poll_delay = function() {
		var req = new Request.HTML({
			onComplete: function(response) {				
				poll_container.setStyle('background-image','none');
				poll_div.set('html','');
				poll_div.adopt(response);	
				jQuery("#poll_result" + poll_id).fadeIn("fast",function(){animateResults(poll_id,w);});
			}
		}).get(root_path + fpoll_url + param);
	}

	poll_delay.delay(500);
}

// Sexy Messeage

function sexymess(mess,href,type) {

	switch(type) {
		case 1: //confirm
				Sexy.confirm(mess, { onComplete: function(returnvalue) { 
            		if(returnvalue)
            			{
							location.href = href;
						}
					}
	  			}); 
				break;
		case 2:  // Force
				Sexy.info('mess', { onComplete: function(returnvalue) {  
						location.href = href;			
					}
	  	 		 });
				 break;
	}
}

// Noob Slide

function initnoobslide(start) {

	var nS1 = new noobSlide({
			startItem: start,
			box: $('main_slider'),
			items: $$('#main_slider div'),
			size: 665,
			fade: false,			
			handles: $$('#handles4 span'),			
			onWalk: function(currentItem,currentHandle){
				//$('main_slide_info').set('html',currentItem.getFirst().innerHTML);
				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});
/*var nS_inner_sponsors = new noobSlide({
			box: $('inner_slider_sponsors'),
			items: $$('#inner_slider_sponsors div'),
			size: 665,
			interval: 10000,		
			fade: true,	
			autoPlay: true,		
			handles: $$('#inner_handles_sponsors span'),			
			onWalk: function(currentItem,currentHandle){
				//$('main_slide_info').set('html',currentItem.getFirst().innerHTML);
				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});*/
var nS_inner_other_news = new noobSlide({
			box: $('inner_slider_other_news'),
			items: $$('#inner_slider_other_news div'),
			size: 665,
			interval: 10000,
			fade: true,	
			autoPlay: true,			
			handles: $$('#inner_handles_other_news span'),			
			onWalk: function(currentItem,currentHandle){
				//$('main_slide_info').set('html',currentItem.getFirst().innerHTML);
				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});	
var nS_inner_newsletter = new noobSlide({
			box: $('inner_slider_newsletter'),
			items: $$('#inner_slider_newsletter div'),
			size: 665,
			interval: 10000,
			fade: true,	
			autoPlay: true,		
			handles: $$('#inner_handles_newsletter span'),			
			onWalk: function(currentItem,currentHandle){
				//$('main_slide_info').set('html',currentItem.getFirst().innerHTML);
				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});	
var nS_inner_focus = new noobSlide({
			box: $('inner_slider_focus'),
			items: $$('#inner_slider_focus div'),
			size: 665,
			interval: 10000,
			fade: true,	
			autoPlay: true,		
			handles: $$('#inner_handles_focus span'),			
			onWalk: function(currentItem,currentHandle){
				//$('main_slide_info').set('html',currentItem.getFirst().innerHTML);
				this.handles.removeClass('active');
				currentHandle.addClass('active');
			}
		});	
}

// Copy to clipboard

function scopy(glueid,contents,type) {
	if (document.getElementById(glueid)) {
  		var clip = new ZeroClipboard.Client();  			
  		clip.glue(glueid);
		if (type==1) {
  			var txt = jQuery.trim(jQuery("#"+contents).text());
		}
		else
		{
			var txt = contents;
		}			
  			clip.setText(txt);
 			clip.addEventListener('complete', function(client, text) {
        		Sexy.alert("Copied text to clipboard: <br />" + "<b>" + text + "</b>");
	  		});
 	}
}

function hover_entry(cssclass) {
	jQuery(cssclass).hover(function() {
				jQuery(this).find('.entry_list_hover').fadeIn('fast');
			}, function() {
				jQuery(this).find('.entry_list_hover').fadeOut('fast');				
			});
}

function toggle_div(class1,class2) {

	$$(class1).addEvent('click', function(e) {
			new Event(e).stop();
			var tdiv = $(this).getNext().getElement('.togglediv');
			var thead = $(this);
			var mySlide = new Fx.Slide(tdiv, {duration: 300});
			if (!thead.hasClass("active") ){
				tdiv.setStyle('display','block')
				$$(".togglediv").removeClass("toggled");
				tdiv.addClass("toggled");
				mySlide.slideIn().chain(function() 
				{
					$$(class2).removeClass("active");
					thead.addClass("active");					
				});
				$$(".togglediv").each(function(togdiv) {
					var toged = togdiv.hasClass("toggled");					
					if (!toged) {
						var myOut = new Fx.Slide(togdiv, {duration: 300});
						myOut.slideOut().chain(function() {
							togdiv.setStyle('display','none')
						});
					}
				});
			}
			else {				
				tdiv.removeClass("toggled");
				mySlide.slideOut().chain(function() 
				{
					thead.removeClass("active");
					tdiv.setStyle('display','none')
				});
			}			
		return false;
	});	
}

function choose_w(id,word) {
	$(id).getElement('.sbin').set('value',word);
	$(id).getElement('div').setStyle('display','none');
}

// Look up words
window.addEvent('domready', function() {
	$$('.checkw').addEvent('keyup',function(e){
		new Event(e).stop();
		var input_value = $(this).get('value');
		dym = $(this).getParent('form').getElement('div');
		id = $(this).getParent('form').get('id')
		if(input_value.clean().length > 3){		
			//build the request
			var req = new Request.JSON({
				url: root_path + flook_url,
				onComplete: function(response){					
					if(response.is_ok && response.is_wrong){							
						dym.setStyle('display','block');
						dym.set('html','Do you mean: <span class="correct"><a onclick="choose_w(\''+id+'\',\''+response.correct_word+'\')">' + response.correct_word +'</a></span> ?');
					} 
						else { dym.setStyle('display','none'); }
				}
			}).post({'word' : $(this).get('value')});
		} else { dym.setStyle('display','none'); }				
	});
	
	$$('.checkw').addEvent('click',function(e){
	   		new Event(e).stop();
			dym = $(this).getParent('form').getElement('div');
			dym.setStyle('display','none');
		   	dym.set('html','');
	});
});

// Look up video
function choose_vid(vid_file) {
	//make the ajax call
	$$('.vid_load').setStyle('display','inline');
	$('video_script').set('opacity',0.0);
	var myslide = new Fx.Tween( $('video_script'), { duration: 250 } );			
		var req = new Request.JSON({
			url: abs_root + fvidectv_url,
			onComplete: function(response) {
				if (response.is_ok) {
					$$('.vid_load').setStyle('display','none');
					$$('.comments').set('html',response.comments);
					$$('.ratings').set('html',response.ratings);
					$$('.poster').set('html',response.author);
					$$('.ectv_sharelink.facebook').set('href','http://www.facebook.com/share.php?u=' + response.video_url);
					$$('.ectv_sharelink.twitter').set('href','http://twitter.com/home?status=Just visited ' + response.video_url);
					$('ectv_share_forum').set('value',response.videoname + ' - ' + response.video_url);
					if (response.has_script) {
						$('video_script').setStyle('display','block');
						myslide.start('opacity',1.0).chain(function() {
							$$('.script_holder').set('html',response.script);
						});							
					} else {
						$('video_script').setStyle('display','none');}					
					$$('a[rel~=external]').set('target','_blank');
					var rating = new RabidRatings({url: abs_root + 'engine/ajax/ratings.php'});
				} else {
				}
			}
		}).post({'vid_file' : vid_file});
}