/*window.addEvent('domready', function(){
	//alert('first');
var myVerticalSlide = new Fx.Slide('fx_share');
alert('second');
$('fx_share_trigger').addEvent('click', function(e){
		e.stop();
		myVerticalSlide.toggle();
});
});*/
var cc_fx_adv_src = new Class({
	Implements:[Options, Events],
	options:{
		
	},
	initialize: function(the_trigger, the_dropper_top, the_dropper_bottom){
		
		this.expanded = true;
		this.the_trigger = $(the_trigger);
		//alert($('search-main').getStyle('height'));
		
		if(this.the_trigger == null || this.the_trigger == undefined)
		{
			return;
		}
		this.the_dropper_top = $(the_dropper_top);
		this.the_dropper_bottom = $(the_dropper_bottom);
		this.to_top = 68;
		this.to_bottom = 250;
		/*this.to_top = this.the_dropper_top.offsetHeight;
		this.to_bottom = this.the_dropper_bottom.offsetHeight;*/
		//alert(this.to_top);
		this.the_dropper_top.setStyle('height', '0px');
		this.the_dropper_bottom.setStyle('height', '0px');
		this.the_trigger.addEvent('click', function(event){
			
			event.preventDefault();
			
			this.toggle();
		}.bind(this));
		this.changer = $('fx_src_changer');
		this.setSelectOptions($('search_in'), $(this.changer.options[this.changer.selectedIndex].value));
		//$('search_in').options=$(this.changer.options[this.changer.selectedIndex].value).options;
		$('most-recent').addEvent('click',function(event){
			//alert(this.checked);
			$('search-sort-time').className="hidd";
		});
		$('being-watched').addEvent('click',function(event){
			//alert(this.checked);
			$('search-sort-time').className="hidd";
		});
		
		
		$('top-rated').addEvent('click',function(event){
			//alert(this.checked);
			$('search-sort-time').className="";
		});
		$('most-viewed').addEvent('click',function(event){
			//alert(this.checked);
			$('search-sort-time').className="";
		});
		$('most-commented').addEvent('click',function(event){
			//alert(this.checked);
			$('search-sort-time').className="";
		});
		
		if($('most-commented').checked || $('most-viewed').checked || $('top-rated').checked)
		{$('search-sort-time').className="";}
		else
		{$('search-sort-time').className="hidd";}
		this.changer.addEvent('change', function(event){
			//alert($(this.options[this.selectedIndex].value).innerHTML);
			if(this.changer.options[this.changer.selectedIndex].value == 'search_in_videos' || this.changer.options[this.changer.selectedIndex].value == 'search_in_audios' )
			{
				$('search-sort-lenght').className="";
			}
			else
			{
				$('search-sort-lenght').className="hidd";
			}
			//$('search_in').options=$(this.options[this.selectedIndex].value).options;
			this.setSelectOptions($('search_in'), $(this.changer.options[this.changer.selectedIndex].value));
			//$('search_in').innerHTML="<option>asd</option>";
		}.bind(this));
		
	},
	setSelectOptions: function(dest, source)
	{
		//alert(dest.length+' - '+source.length);
		var j=dest.length;
		//alert(j);
		
		for(var i=0; i<j; i++)
		{
			dest.remove(0);
			//alert(i);
			
		}
		j = source.length
		i = 0;
		
		for (i=0; i<j; i++)
		{
			//alert(i);
				var x = source.options[i];
				oo = document.createElement("option");
				oo.value = x.value;
				oo.text = x.text;
				oo.selected = x.selected;
				
				try
				{
					dest.add(oo,null);
				}
				catch(ex)
				{
					dest.add(oo, i);
				}
		}
		
	},
	
	toggle: function(){
		var to_top;
		var to_bottom;
		
		if(this.expanded)
		{
			this.expanded = false;
			//to_top = '50px';
			//to_bottom = '110px';
			to_top = this.to_top;
			to_bottom = this.to_bottom;
			
		}
		else
		{
			this.expanded = true;
			to_top = '0px';
			to_bottom = '0px';
		}
		this.the_dropper_top.set('tween', {
			duration:1000,
			transition: Fx.Transitions.Cubic.easeOut
		}).tween('height', to_top);
		this.the_dropper_bottom.set('tween', {
			duration:1000,
			transition: Fx.Transitions.Cubic.easeOut
		}).tween('height', to_bottom);
		
	}
});
var cc_fx_related_drop = new Class({
	Implements:[Options, Events],
	options:{
		
	},
	initialize: function(the_trigger, the_dropper){
		
		this.expanded = true;
		this.the_trigger = $(the_trigger);
		this.the_dropper = $(the_dropper);
		
		this.the_trigger.addEvent('click', function(event){
			event.preventDefault();
			this.toggle();
		}.bind(this));
	},
	toggle: function(){
		
		var to;
		if(this.expanded)
		{
			this.expanded = false;
			to = '0px';
		}
		else
		{
			this.expanded = true;
			to = '319px';
		}
		this.the_dropper.set('tween', {
			duration:1000,
			transition: Fx.Transitions.Bounce.easeOut
		}).tween('height', to);
		
	}
});
var cc_fx_comments = new Class({
	
	Implements:[Options,Events],
	options:{
		
	},
	initialize: function(container){
		//load the first 10 comments comments
		this.loading = false;
		this.container = $(container);
		this.loading = true;
		this.baseurl = $('base_url').href;
		var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var string_length = 8;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
		this.rs = randomstring;
		//alert(this.baseurl);
		this.load(this.baseurl + '0'+'/'+this.rs);
		//this.container.load('http://test.clip-center.com/code-clip-center2/comments/video/1/0').chain(function(){this.loading = false}.bind(this));
		//this.attach(container);
	},
	/*attach: function(){
		//alert(container);
		var pages = this.container.getElements('a');
		alert(pages);
		pages.each(function(page){
			page.addEvent('click', function(event){
				event.preventDefault();
			});
		});
	},*/
	load: function(link){
		var req = new Request(
                 {
                 	 method: 'get',
                     url: link,
                     onComplete: function(response)
                         {
                         	 var myFx = new Fx.Tween(this.container);
							 myFx.set('opacity', '0').chain(function(){alert('asd');});
                         	 this.container.set('html', response);
                         	 this.container.fade(1);
                         	 if($chk($('ajax_comments_pag')))
                         	 {
                                 links = $('ajax_comments_pag').getElements('a');
                                 links.each(function(lnk){
									 lnk.addEvent('click', function(event){
									 	 event.preventDefault();
									 	 var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
		var string_length = 8;
		var randomstring = '';
		for (var i=0; i<string_length; i++) {
			var rnum = Math.floor(Math.random() * chars.length);
			randomstring += chars.substring(rnum,rnum+1);
		}
										 this.load(lnk+'/'+randomstring)
									 }.bind(this))
                                 }.bind(this));
							 }
                             var st = $$('.extra_comm_trigger');
                             st.each(function(s){
								s.addEvent('click', function(e){
									e.stop();
									var pcSlide = new Fx.Slide('fx_comment_form');
									pcSlide.slideIn().chain(function(){$('fx_comment_form').fade(1); setTimeout("document.getElementById('comment_content').focus()", 300);});
								}) ;
                             });
                         }.bind(this)
                         
                 }).send();
	}
	
});
//general ajax tabs
var cc_fx_ajax_tabs = new Class({
	Implements:[Options,Events],
	options:{
		
	},
	initialize: function(triggers, conts, classname){
		this.triggers = triggers;
		this.conts = conts;
		this.classname=classname;
		this.triggers.each(function(trigger){
			trigger.addEvent('click', function(e){
				e.stop();
				
				var i=this.triggers.indexOf(trigger);
				this.conts.each(function(c){
					c.style.display='none';
				});
				this.conts[i].style.display='block';
				this.triggers.each(function(t){
					t.className=this.classname;
				}.bind(this));
				this.triggers[i].className = this.classname+'_active';
				$('home_tabs_heading_caps_1').innerHTML = trigger.innerHTML;
				$('home_tabs_heading_caps_2').innerHTML = trigger.innerHTML;
			}.bind(this));
		},this);
		
	}
	
});
var cc_fx_tabs = new Class({
	Implements:[Options,Events],
	options:{
		
	},
	initialize: function(container, links, drop_container){
		this.container = $(container);
		this.links = $$(links);
		this.theindex = -1;
		this.drop_container = drop_container;
		this.fading = false;
		this.slide = new Fx.Slide(container,{
		    duration: 700,
		    transition: Fx.Transitions.Pow.easeOut
		});
		this.container.fade(0.1);
		this.slide.hide();
		this.attach();
		
	},
	formsubmit:function(gt){
		//alert( this.container.getElements('form')[0].get('html') );
		this.container.getElements('form')[0].addEvent('submit', function(e) {
			//Prevents the default submit event from loading a new page.
			e.stop();
			//Empty the log and show the spinning indicator.
			//var log = $('log_res').empty().addClass('ajax-loading');
			//Set the options of the form's Request handler. 
			//("this" refers to the $('myForm') element).
			this.set('send', {onComplete: function(response) { 
				//log.removeClass('ajax-loading');
				//alert(response);
				//gt.myslideout();
				//slide out
				this.innerHTML=response;
				
				
			}.bind(this) } );
			
			
			
			//Send the form.
			//alert(response);
			this.send();
		});
	},
	myslideout: function(){
			//alert('single slide out');
			this.slide.slideOut().chain(function(){this.fading=false}.bind(this));
			this.container.fade(0.3);
			this.links[this.theindex].set('class', 'fx_trigger_inactive');
			this.theindex = -1;
	},
	attach: function(){
		this.links.each(function(link){
			link.addEvent('click', function(event){
				event.preventDefault();
				if(this.fading) return false;
				//this.container.load(link.get('href'));
				//alert (this.links.length);
				this.fading = true;
				
				//if the index < 0 there is nothing shown
				if(this.theindex<0)
				{
					this.theindex = this.links.indexOf(link);
					//this.container.set('html',this.links[this.theindex],this.links[this.theindex],this.links[this.theindex]);
					
					this.container.set('html', $(''+this.drop_container+this.theindex).get('html'));
					this.formsubmit(this);
					this.slide.slideIn().chain(function(){this.fading=false;}.bind(this));
					this.container.fade(1);
					this.links[this.theindex].set('class', 'fx_trigger_active');
				}
				else if( this.theindex != this.links.indexOf(link) )
				{
					this.links[this.theindex].set('class', 'fx_trigger_inactive');
					//slideout, slide in
					this.slide.slideOut().chain(function(){  this.container.set('html', $(this.drop_container+this.theindex).get('html'));this.formsubmit(this);  this.slide.slideIn().chain(function(){this.fading=false; this.links[this.theindex].set('class', 'fx_trigger_active'); }.bind(this))}.bind(this));
					this.container.fade(0.3);
					this.theindex = this.links.indexOf(link);
					this.links[this.theindex].set('class', 'fx_trigger_active');
					//this.links[this.theindex].getParent().set('class', 'fx_trigger_active');
					//alert(this.links[this.theindex].get('html'));
					//this.container.load(this.links[this.theindex]);
					
					
					//this.slide.slideIn();
					this.container.fade(1);
				}
				else
				{
					//slide out
					
					//this.container.set('html', 'empty');
					this.slide.slideOut().chain(function(){this.fading=false}.bind(this));
					this.container.fade(0.3);
					this.links[this.theindex].set('class', 'fx_trigger_inactive');
					//this.links[this.theindex].getParent().set('class', 'fx_trigger_inactive');
					this.theindex = -1;
					
				}
				
				
				/*if(this.theindex >= 0 && (this.theindex != this.links.indexOf(link)) )
				{
					this.slide.slideOut();
					this.container.fade(0.1);
					this.theindex = this.links.indexOf(link);
					//this.container.load(this.links[this.theindex]);
					this.container.set('html',this.links[this.theindex],this.links[this.theindex],this.links[this.theindex]);
					this.slide.slideIn();
					this.container.fade(1);
				}*/
				/*if(this.theindex < 0)
				{
					this.theindex = this.links.indexOf(link);
					//this.container.load(this.links[this.theindex]);
					this.container.set('html',this.links[this.theindex],this.links[this.theindex],this.links[this.theindex]);
					this.slide.slideIn();
					this.container.fade(1);
					
					//alert(this.theindex);
					
				}*/
				/*else
				{
					this.container.set('html', 'empty');
					this.slide.slideOut();
					this.container.fade(0.1);
					this.theindex = -1;
				}*/
			}.bind(this));
		}, this);
	}
});
window.addEvent('domready', function(){
	//new cc_fx_tabs($('fx_share'),$$('#fx_triggers a'));
	//new cc_fx_comments('the_comments');
	
	var scroller = new Fx.Scroll(document.body);
if($chk($('return_top'))){
$('return_top').addEvent('click', function(e){
	e.stop();
	scroller.toTop();
})};
	
	new cc_fx_adv_src('fx_adv_src_trigger', 'search_a_top', 'search_bottom');
	if($chk($('languages')))
	{
		var llis = $('languages').getElements('li');
		var ls = llis.length;
		llis.each(function(lli){
			lli.addEvent('click', function(event){
				event.preventDefault();
				
				$('language_hidden_input').set('value', this.title)
				$('language_hidden_form').submit();
			})
		});
	}
	if($('home_tab_holder'))
	{
		new cc_fx_ajax_tabs($$('div#home_tab_holder a'), $$('div.home_tab_c'), 'home_tab');
	}
	
	if($chk($('language_choose'))){
	//language selector (dd)
		$('language_choose').addEvents({
			'mouseenter': function(){
				// Always sets the duration of the tween to 1000 ms and a bouncing transition
				// And then tweens the height of the element
				this.set('tween', {
					duration: 1000,
					transition: Fx.Transitions.Bounce.easeOut // This could have been also 'bounce:out'
				}).tween('height', 19*(ls-1)+16 + 'px');
				//alert (ls);
			},
			'mouseleave': function(){
				// Resets the tween and changes the element back to its original size
				this.set('tween', {}).tween('height', '16px');
			}
		});
	}
	/*$('related_videos_fx_trigger').addEvent('click', function(event){
		event.preventDefault();
		$('related_video_scroll').set('tween', {
			duration:1000,
			transition: Fx.Transitions.Bounce.easeOut
		}).tween('height', '519px')
	});*/
	//change language
	
	
	
	
	/*links.each(function(lnk){
									 lnk.addEvent('click', function(event){
									 	 event.preventDefault();
										 this.load(lnk)
									 }.bind(this))
                                 }.bind(this));*/
	//video thumbs
	
	var pt_thumbs = $$('.image_video_thumb img');
	pt_thumbs.each(function(img){
		img.inside = true;
		img.completed = false;
		img.orig = '';
		img.orig_path='';
		var file = img.src.substring(img.src.lastIndexOf('/')+1,img.src.length);
		var path = img.src.substring(0, img.src.lastIndexOf('/')+1);
		
		
		img.orig = file;
		var file2 = file.substring(file.lastIndexOf('_')+1, file.length);
		img.orig2 = file2;
		
		
		img.orig_p = path;
		img.addEvent('mouseover', function() { 
			
			//alert(path);
			new Asset.images(
				[path+'1_'+file2, path+'2_'+file2, path+'3_'+file2],{
				onComplete: function (){
					//img.src=path+'3_'+file;
					
					
					
					img.completed = true;
					//img.inside = true;
					/*var cc = function(){this.start(1)}.bind(this);
					cc.delay(1000);*/
					if(img.inside == true)
					{
						
						
						img.s(1);
						
					}
					else
					{
						img.stop();
					}
					//img.inside = true;
				}
				
			});
		});
		img.addEvent('mouseenter', function(){
			if(img.completed == true)
			{
				img.inside = true
				
				
				this.s(1);
				
				
			}
			
		});
		img.addEvent('mouseout', function() { img.stop(); });
		img.s=function(pre)
		{
			
			if( (this.inside == true) && (pre<10) )
			{
				this.src = this.src.substring(0, this.src.lastIndexOf('/')+1)+( (pre%3)+1 )+'_'+this.orig2;
				//alert(this.orig2);
				var cc = function(){this.s(pre+1)}.bind(this);
				cc.delay(700);
				
			}
			else
			{
				this.stop();
			}
			
		};
		img.stop=function()
		{
			
			this.src = this.orig_p + this.orig;
			
			this.inside = false;
		};
		
		//alert(file);
	});
});
//autocomplete part
/*
document.addEvent('domready', function() {
	
	var inputWord2 = $('search-field');
 
	// Our instance for the element with id "demo-word2"
	new Autocompleter.Request.HTML(inputWord2, baseurl+'ajax/search', {
		'indicatorClass': 'autocompleter-loading',
		'postData': {
			'extended': '1' // send additional POST data, check the PHP code
		},
		'injectChoice': function(choice) {
			// choice is one <li> element
			var text = choice.getFirst();
			// the first element in this <li> is the <span> with the text
			var value = text.innerHTML;
			// inputValue saves value of the element for later selection
			choice.inputValue = value;
			// overrides the html with the marked query value (wrapped in a <span>)
			text.set('html', this.markQueryValue(value));
			// add the mouse events to the <li> element
			this.addChoiceEvents(choice);
		}
	});
 
});*/
