/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/*
Copyright 2006-2010 Malek Nasser and iFLYER.tv All rights reserved. Distribution or
reuse of any part of this code without express written consent from
Malek Nasser and iFLYER is strictly forbidden. Violators will be prosecuted to the
maximum extent afforded by law. (looking at you Clubberia, Andrew Winter and Sho Fujino)
*/
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/
/**************************************************************************************************************************/


// there are some problems with mootools default function so this is a replacement, bug appears for chrome

Request.HTML.implement({

	processHTML: function(text){
		var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
		text = (match) ? match[1] : text;
	   
		var container = new Element('div');
	   
		return $try(function(){
			var root = '<root>' + text + '</root>', doc;
			if (Browser.Engine.trident){
				doc = new ActiveXObject('Microsoft.XMLDOM');
				doc.async = false;
				doc.loadXML(root);
			} else {
				doc = new DOMParser().parseFromString(root, 'text/html');
			}
			root = doc.getElementsByTagName('root')[0];
			for (var i = 0, k = root.childNodes.length; i < k; i++){
				var child = Element.clone(root.childNodes[i], true, true);
				if (child) container.grab(child);
			}
			return container;
		}) || container.set('html', text);
	}

});

var top_pickups_loaded = false;

window.addEvent('domready', function() {
									 
							 
/*
	new showHider({
		target: $("city_select"),
		link: $("city_select_link"),
		target_height: 28
	});
*/
	
	enhancedHeaderSearch();
	
	$$(".block_options .option").each( function(item) {
		item.addEvent("click",function(e) {
			this.getParent(".block_options").getElements('.option').removeClass('selected');
			this.addClass('selected');
		})
	});
	
	
	$$(".un_grey_over").each( function(item) {
		item.addEvent("mouseover",function(e) {
			this.src = this.src.replace("/gs_1/","/nogrey_1/");
		}),
		item.addEvent("mouseout",function(e) {
			this.src = this.src.replace("/nogrey_1/","/gs_1/");
		})
	});
	
	
	$$(".toplink").each( function(item) {
		item.addEvent("click",function(e) {
			e = new Event(e);
            e.stop();
			new Fx.Scroll(window, {wait:false, duration:500}).toTop();
		})
	});
	
	
	if($$(".showmenus_hidden")!=""){
		$$(".showmenus_hidden").each( function(item) {
			if(item.getElement('.showmenus_hidden_content')){
				item.addEvent("mouseover",function() {
					item.getElement('.showmenus_hidden_content').setStyle("display","block");
				});
				item.addEvent("mouseout",function() {
					item.getElement('.showmenus_hidden_content').setStyle("display","none");
				});
			}
		});
	}
	
	
	$$(".ajax_loader").each( function(item) {
		if($(item.get('rel')) && item.id && item.get('ajaxhref')){
			
			if(item.hasClass('ajax_click')){
				item.addEvent("click",function() {							   
					if(window[item.id]!=item.get('ajaxhref'))	{
						
						$(item.get('rel')).set("html","<div class=\"loading_section loading_over\">Loading</div>"+$(item.get('rel')).get("html"));
						new Request.HTML({
							url: item.get('ajaxhref'),  
							update: $(item.get('rel')),
							method: 'post', 
							onComplete: function() { pageRefresh();  },
							link: 'cancel'
						}).send();
						item.removeEvent("mouseover");
						window[item.id] = item.get('ajaxhref');
					}
				});
			}else{
				item.addEvent("mouseover",function() {							   
					if(window[item.id]!=item.get('ajaxhref'))	{
						
						$(item.get('rel')).set("html","<div class=\"loading_section\">Loading</div>");
						new Request.HTML({
							url: item.get('ajaxhref'),  
							update: $(item.get('rel')),
							method: 'post', 
							link: 'cancel'
						}).send();
						item.removeEvent("mouseover");
						window[item.id] = item.get('ajaxhref');
					}
				});
			}
				
				
		}
	});


	
	$$(".list_options .menu_selected").addEvent("click",function() {
		
		$linkUnit = this;
		$divUnit = this.getParent(".list_option").getChildren(".list_options_div");

		$testOpen = $divUnit.getStyle("display");
		
		if($testOpen=="block"){
			$divUnit.setStyle("display","none");
			$linkUnit.removeClass("menu_highlighted");
		}else{
			$divUnit.setStyle("display","block");
			$linkUnit.addClass("menu_highlighted");
		}
		
	});
	

	$$(".list_options .list_option").addEvent("mouseleave",function() {
		$linkUnit = this.getChildren(".menu_selected");
		$divUnit = this.getChildren(".list_options_div");
		
		$divUnit.setStyle("display","none");
		$linkUnit.removeClass("menu_highlighted");
	});
	
	
	$$(".lang_options").each(function(el){
		el.getElements("li").each(function(el2){
			if(!el2.hasClass('disabled')){
				el2.addEvent("click",function(e) {
					if(!el2.hasClass('selected')){
						el.getElements("li").removeClass('selected');
						show_lang(el.get('id'),el2.get('rel'));
						el2.addClass('selected');
					}
				});
			}
		});
	});

	
	if(window.greatFonts) {

	greatFonts();
	
	}
	
	/*  AUTO TICKER FUNCTIONS */
	
	
	
	if($$('.auto-ticker')!=""){
		
		autoTickerTimer = autoTickerFlick.periodical(5000);	
		
		$$('.auto-ticker').addEvents({ 
			mouseenter: function(){
				window[this.id+"_autotick"]['tick']=false;
			},
			mouseleave: function(){
				window[this.id+"_autotick"]['tick']=true;
				if(window[this.id+"_autotick"]['thisdelay']){
					window[this.id+"_autotick"]['thisdelay'] = 1;
				}
			}
		});
	}
	
	
	$$('.read_more').each(function(el){
		
		$testHeight = el.getStyle('height').toInt();
		//el.getElements("object").each(function(test){$testHeight+=test.getStyle('height').toInt();});
		if($testHeight > el.get('rel').toInt()){
			el.setStyle('height',el.get('rel')+"px");
			
			el.getElement('.read_more_tempt').setStyle('display','block');
			el.getElement('.read_more_tempt').addEvent("click",function(el2) {	
															$addHeight = el.getStyle('padding-top').toInt() + el.getStyle('padding-bottom').toInt();
															this.set('tween', {
															duration: 500
															}).tween('opacity', [1,0]);	
															el.set('tween', {
															duration: 500
															}).tween('height', el.getElement(".read_true").getStyle("height").toInt()+$addHeight);	
															// el.getElement(".read_true").getStyle("height").toInt()+$addHeight+20
														});
		}
	});

	//Scroll
	var ss = new SmoothScroll({ duration:500 }, window);
	
	//EP Content
	//var ep_content = new epContent();
	
	if($$('.fader')!=""){
		switchFade = switchFade.periodical(5000);
	}
	
	
	//if($$('.iflyer_gallery')!=""){
		
		var addGallery = new Element('div', {
						'id': 'ifg-container',
						'styles': {
							'display': 'none'
						}
						/*,
						'events': {
							'click': function(){
								alert('clicked');
							},
							'mouseover': function(){
								alert('mouseovered');
							}
						}*/
		});
		
		var addGalleryTitle = new Element('div', {
						'id': 'ifg-info',
						'text': 'Loading gallery'
		});
		
		var addGalleryOverlay = new Element('div', {
						'id': 'ifg-overlay'
		});
		
		var addGalleryData = new Element('div', {
						'id': 'ifg-data'
		});
	

		addGallery.adopt([addGalleryTitle, addGalleryOverlay, addGalleryData]);
		addGallery.inject(document.body);

		
		$$('.iflyer_gallery').each(function(el){
				el.removeEvents();
				el.addEvent("click",function(e) {
						if(el.get('gallery')){
							loadGallery(el.get('gallery'));
						}else{
							loadGallery(el.get('href'));
						}
						return false;
				});
		});
	
	//}
		if($$('.show_all')!=""){
		$$('.show_all').each(function(el){
							el.addEvent('click', function(e){
												    $parent = el.getParent(".show_all_holder");
													$parent.setStyle('opacity',0.1);
													$parent.addClass("over_show_all");
													$parent.set('tween', {duration: 200}).tween('opacity', [0,1]);
													return false;
												});
							});
		}
		
		if($$('.show_all_holder')!=""){
		$$('.show_all_holder').addEvent('mouseleave', function(){
													this.removeClass('over_show_all');
												});
		}
	
	if(window.defaultLoadGallery){
		//alert(defaultLoadGallery);
		loadGallery(defaultLoadGallery);	
	}
	
	
	/*
	
	if(window.FB_RequireFeatures) {
	
	FB_RequireFeatures(["Comments"], function() { 
			FB.CommentClient.add_onComment(function(comment){ 
			if($('iftv-js-comment')){
				new Request({url:$('iftv-js-comment').get('rel')}).send();
			}
			//alert("user " + comment.user + " wrote: " + comment.post); 
		}); 
	});
	
	}
	
	*/
	

	adjustFooterHeight()
	window.addEvent("resize", adjustFooterHeight);
	/*
	(function() {
	alert(1);
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +  '//connect.facebook.net/en_US/all.js'; //<?=($this->getOutputLanguage()=="ja")?"ja_JP":"en_US"?>
    document.getElementById('fb-root').appendChild(e);
  	}());
	*/
	
	
});


function adjustCommentAdd(){
	// at the moment we dont detect who commented, but this might change
	if($('iftv-js-comment')){
		new Request({url:$('iftv-js-comment').get('rel')}).send();
	}	
}

function adjustFacebookStats(){
	if(!fbSessionId){
		// alert("error, no user id");
	}else{
		if(fbSessionLike){
			//alert(fbSessionLike+":"+fbSessionId);
			curData = {
			'href' : fbSessionLike,
			'uid' : fbSessionId
			};
			new Request({url:"/api/facebook/like",method: 'post',data: curData}).send();
		}
	}
}


function adjustFooterHeight(){
	
	if(!$('new_header')){
		window.removeEvent("resize");
		return false;	
	}
	
	$screenheight = screen.height.toInt();

	$window = window.document.body.getSize();
	$window = $window.y.toInt();

	
	$footerPos = $('new_header').getStyle('height').toInt()+$('wrapper').getStyle('height').toInt()+$('footer').getStyle('height').toInt()+41;
	
	if($footerPos>=$screenheight){
		window.removeEvent("resize", adjustFooterHeight);
	}
	
	if($window>$footerPos){
		$('wrapper').setStyle('height',$window-$footerPos+$('wrapper').getStyle('height').toInt());
	}

}


function pageRefresh(){
	Shadowbox.setup("a.shadowbox");
	recheckgallerys();
	
	if($$('.show_all')!=""){
		$$('.show_all').each(function(el){
						el.addEvent('click', function(e){
												$parent = el.getParent(".show_all_holder");
												$parent.setStyle('opacity',0.1);
												$parent.addClass("over_show_all");
												$parent.set('tween', {duration: 200}).tween('opacity', [0,1]);
												return false;
											});
						});
	}
	
	if($$('.show_all_holder')!=""){
	$$('.show_all_holder').addEvent('mouseleave', function(){
												this.removeClass('over_show_all');
											});
	}
	
	if(window.addHoverCard) {
		addHoverCard();
	}
	
	if(window.FB){
		updateFacebook();
	}
	
}


function recheckgallerys(){
	
	$$('.iflyer_gallery').each(function(el){
				el.removeEvents();
				el.addEvent("click",function(e) {
						if(el.get('gallery')){
							loadGallery(el.get('gallery'));
						}else{
							loadGallery(el.get('href'));
						}
						return false;
				});
		});	
	
}


function strpos (haystack, needle, offset) {
	var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

var galleryTimeOut;

loadGallery = function($href){
	if(!$href.contains('/gallery')){/*need no gallery found error here*/return false;}

	$galleryHref = $href.substr(strpos($href,'/gallery',0));
	$fromPage = $href.substr(0,strpos($href,'/gallery',0));
	
	window.addEvent("keydown", galleryKeys);
	
	$('ifg-container').setStyle('opacity',1);
	$('ifg-container').setStyle('display','block');
	
	$('ifg-info').setStyle('opacity',1);
	$('ifg-info').setStyle("display","block");
	/*$('ifg-overlay').setStyle("display","block");*/
	
	
	$('ifg-overlay').setStyle('opacity',0);
	$('ifg-overlay').set('tween', {
				duration: 500
				}).tween('opacity', [0,0.8]);
	$('ifg-overlay').setStyle("display","block");
	
	
	galleryTimeOut = $clear(galleryTimeOut);
	curData = {
			'fromPage' : $fromPage,
			'onpage' : 'true'
		};
	new Request.HTML({
		url: $galleryHref,
		/*
		onRequest: function() { galleryTimeOut = timeOutGallery.delay(5000,this);  },
		*/
		onComplete: function() { setUpGallery(); },
		update: $('ifg-data'),
		method: 'post', 
		link: 'cancel',
		data: curData
	}).send();

};

function setUpGallery(){
	
	if($('ifg-featured')==""){
			closeGallery();
	}

	$('ifg-info').set('tween', {
			duration: 500
			}).tween('opacity', [1,0]);/*upcomingTimeOut = $clear(upcomingTimeOut); Shadowbox.setup("a.shadowbox");  alert('hi');*/ 
	$('ifg-data').setStyle('opacity',0);
	$('ifg-data').setStyle("display","block");
	$('ifg-data').set('tween', {
			duration: 500
			}).tween('opacity', [0,1]);
	
	
	$('ifg-container').removeEvents("mousewheel");
	$('ifg-container').addEvents({ 
		mousewheel: function(e){
			moveThumb(-e.wheel);
			return false;
		}
	});
	

	$('ifg-featured').getElement('img').addEvent("click", function(e){ moveThumb(); return false; });

	
	window.addEvent("keydown", galleryKeys);
	window.addEvent("resize", adjustGallyerHeight ); 
	
	$('ifg-overlay').addEvent("click", closeGallery_part1);
	$('ifg-featured').addEvent("click", closeGallery_part1);
	$('ifg-close').addEvent("click", closeGallery_part1);
	
	moveThumbs($('ifg-thumbs').getElement('.selected'));
	
	$$('#ifg-thumbs li').addEvent("click", function(e) { moveThumbs(this) });

}

var galleryKeys = function(e){ 
			if(e.key=="esc"){closeGallery_part1();}
			if(e.key=="left"){moveThumb(-1);}
			if(e.key=="right" || e.key=="enter"){moveThumb(1);}
			return false;
}

var closeGallery_part1 = function(e){
		$('ifg-container').set('tween', {
			duration: 500
			}).tween('opacity', [1,0]);								
		closeGallery.delay(500);
}

function closeGallery(){
	$('ifg-info').setStyle("display","none");
	$('ifg-container').setStyle('display','none');
	$('ifg-overlay').setStyle("display","none");
	$('ifg-overlay').removeEvents("click");
	$('ifg-featured').removeEvents("click");
	$$('#ifg-thumbs li').removeEvents("click");
	$('ifg-close').removeEvents("click");
	$('ifg-data').getChildren().destroy();
	$('ifg-container').removeEvents("mousewheel");
	window.removeEvent("keydown", galleryKeys);
	window.removeEvent("resize", adjustGallyerHeight);
}


function moveThumb(where){
		
		if(where<0){
			$testNext = $('ifg-thumbs').getElement('li.selected').getPrevious();
		}else{
			$testNext = $('ifg-thumbs').getElement('li.selected').getNext();
		}

		if($testNext!=null){
			moveThumbs($testNext);
		}

}

var galleryImages;

function moveThumbs(el) {
		$$('#ifg-thumbs li').removeClass('selected');
		$$('#ifg-thumbs li').removeClass('plusone');
		$$('#ifg-thumbs li').removeClass('backone');
		
		el.addClass('selected');
		
		if($('ifg-thumbs').getElement('li.selected').getNext()!=null){
			$('ifg-thumbs').getElement('li.selected').getNext().addClass('plusone');
		}
		
		if($('ifg-thumbs').getElement('li.selected').getPrevious()!=null){
			$('ifg-thumbs').getElement('li.selected').getPrevious().addClass('backone');
		}
		
		//
		$pos = el.getPosition(el.getParent());
		$wid = el.getStyle('margin-right').toInt()+el.getStyle('width').toInt();
		
		/*
		var img = new Image();
		img.src = el.get('rel');
		//size = $('ifg-featured').getElement('img').getSize();
		alert(el.get('rel')+" "+img.width + 'x' + img.height);
alert(el.get('rel')+" "+img.width + 'x' + img.height);
		//alert("The element is "+size.x+" pixels wide and "+size.y+"pixels high.");
*/
		//var myImage = new Asset.image(el.get('rel'), {id: 'myImage', title: 'myImage', onComplete: myTestFunction }); //, onLoad: myTestFunction
		/*myTestFunction();*/
		//alert(myImage);
		$('ifg-stat').set('stat_id',el.get('stat_id'));

		galleryImages = new Asset.images(el.get('rel'), {
			width: '', 
			height: '',
			onComplete: function(){
				adjustGallyerHeight( true );
				
			}
		});
		
		$newShare = "&url="+el.get('sharelink')+"&title="+el.get('sharetitle');		
		$$('#ifg-details .share_links_unit a').each(function(el2){ // update share links
															 	$oldBase = el2.get('href').substr(0,strpos(el2.get('href'),'&url=',0));
															 	el2.set('href',$oldBase+""+$newShare);
																
																});
		
		
		
		
		$('ifg-thumbs').getElement('ul').set('tween', {
			duration: 200
			}).tween('left', -$pos['x']-($wid/2) );
		
		$('ifg-details').getElement('#imgno span').set('html',el.get('pos').toInt()+1);
		
		$('ifg-details').getElement('h2').set('html',el.get('title'));
		//$('ifg-details').getElement('h2').setStyle('margin-left',-$('ifg-featured').getElement('img').getStyle('width').toInt()/2);
}


/*
function timeOutGallery(){
	this.running = false;
	this.xhr.abort();
	$$('#upcoming_results .loading_list_error').setStyle("display","block");
	loadUpcomingList('upcoming_results');
}
*/


/*


*/

function print_r(theObj){
	$printr = "";
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    $printr +="\n<ul>\n";
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
$printr +="\n["+p+"] => "+typeof(theObj)+"";
        $printr +="\n<ul>\n";
        print_r(theObj[p]);
        $printr +="\n</ul>\n";
      } else {
$printr +="\n["+p+"] => "+theObj[p]+"";
      }
    }
    $printr +="\n</ul>\n";
  }
  return $printr;
}



var adjustGallyerHeight = function(reloadFile){

	reloadAgain = false;
	dontresize = false;
	var defaultView = this.getDocument().defaultView;
	if(defaultView){
		myheight = galleryImages.getStyle('height');
		mywidth = galleryImages.getStyle('width');
	}else{
		myheight = 0;	
		mywidth = 0;
	}
	src = galleryImages.get('src');

	myId = galleryImages.get('stat_id');

	$imageY = myheight+" "; // not sure why i need this but seems to convert it to a sting so it can be converted to a number ???? wierd
	$imageY = $imageY.toInt();
	$imageX = mywidth+" ";
	$imageX = $imageX.toInt();
	

	if(!$imageY){

		$currentImage = $('ifg-featured').getElement('img').getSize();
		$imageY = $currentImage.y.toInt();
		$imageX = $currentImage.x.toInt();
		
		if($imageY<30){ // chrome bug
			dontresize = true;
		}
		//$image = 600; // this is a setting for chrome...
		if(reloadFile==true){reloadAgain = true;}
		//return false;
	}

	$window = window.document.body.getSize();
	$window = $window.y.toInt();
	
	$topHeight = ($window-150-$imageY)/2;
	$potentialSpace = $window-124; // not 170 anymore
	if($potentialSpace<320){
		$potentialSpace = 320;	
	}

	if($topHeight>10){
			$newHeight = $imageY;
			$newTop = $topHeight;
	}else{
			$newTop = "10";
			if($imageY>$potentialSpace){
				$newHeight = $potentialSpace;
			}
			
	}
	
	if(reloadFile==true){
		if(reloadAgain==true){
			$get = $('ifg-featured').getElement('img').dispose();
			galleryImages.inject($('ifg-featured'));
			
			$('ifg-featured').getElement('img').addEvent("click", function(e){ moveThumb(); return false; });
			
		}else{
			$('ifg-featured').getElement('img').set('src',src);	
			$('ifg-stat').setStyle('background','url(/gallery/stats/'+$('ifg-stat').get('rel')+":"+$('ifg-stat').get('stat_id')+')');	
		}
	}
	
	
	if(reloadAgain!=true && dontresize != true){
		$ratio = $imageY/$imageX;

		$('ifg-featured').setStyle('margin-top',$newTop+"px");
		if($newHeight<200){$newHeight=320;}
		$('ifg-featured').getElement('img').setStyle('height',$newHeight+"px");	
		$newWidth = ($newHeight/$ratio);
		$newWidth = $newWidth.toInt();
		$('ifg-featured').getElement('img').setStyle('width',$newWidth+"px");	
		
	}
	
	if(reloadAgain){
		adjustGallyerHeight(false);
	}
}


var enhancedHeaderSearch = function(){
	//Site Search dropdown	
	
	
	//Remove/Value stuff
	if(!$("header_search_text")){
		return false;
	}
	
		
	$("header_search_text").addEvent("click",function() {			
		if(!this.hasClass("active")) { 
			this.setProperty("value","");
			this.addClass("active");
		}
	});
	
	$("header_search_text").addEvent("keypress",function(e) {			
		if(e.key=="enter"){
			submitSearch();
			return false;	
		}
	});
	
	
	//Quick search
	addSearchEvent = function(el){
		el.addEvent("click",function(e) {
			e = new Event(e);
			e.stop();
			
			if($("header_search_text").hasClass("active")) {
				/* Perform a search under this catagory */
				if(this.get("type") != "submit"){
					$("input_header_search").setProperty("action",this.get("href"));
				}
				//window.location.href =  $("input_header_search").toQueryString().replace(/[A-Za-z]+?=&|[A-Za-z]+?=$|[&=]/gi, "/").replace(/[\/]{2,}/gi, "/");
				submitSearch();
			} else {				
				/* Select this catagory */
				
				this.addClass("active");			
				
				if(!$("header_search_text").hasClass("active")) { $("header_search_text").setProperty("value",this.getProperty("title")) };
				$("input_header_search").setProperty("action",this.get("href"));	
			}		
		});
		/*
		el.addEvent("mousekey",function(e) {
										
		});
		*/
	};
	
	addSearchEvent($('header_search_button'));
	
	submitSearch = function(){

		window.location.href =  "/search/"+$("header_search_text").value.replace(/[A-Za-z]+?=&|[A-Za-z]+?=$|[&=]/gi, "/").replace(/[\/]{2,}/gi, "/");	
	};
	
	//Change search type
	
}


function show_lang(div_id,lang){
	$(div_id+"_"+lang).setStyle("display","block");
	if(lang=="EN"){anit_lang = "JA";}else{anit_lang = "EN";}
	$(div_id+"_"+anit_lang).setStyle("display","none");
}

function autoTickerFlick(){
	//alert("hi");	
	
	$$('.auto-ticker').each(function(el){
		$tick = true;
		$tickData = window[el.id+"_autotick"];
		$tickOptions = window[el.id+"_options"];
		$tickOptions = $merge($tickOptions, {'reset_on_end':true});
		//alert($tickOptions['reset_on_end']);
		if($tickData['delay'] && $tickData['thisdelay']){
			if($tickData['delay']>$tickData['thisdelay']){
				$tick = false;	
				window[el.id+"_autotick"]['thisdelay']++;
			}else{
				window[el.id+"_autotick"]['thisdelay']=1;
			}
			
			//alert($tickData['delay']);
		}
		if($tickData['tick'] && $tick){
			magicScroll($tickData['el'],$tickData['el_navi'],$tickData['direction'],$tickOptions);
		}
	});
	
	//
}


function sideScroll(el,posX,adder,maxpos,tweentime){

		if(!tweentime){
			tweentime = 500;	
		}

		if(adder==true && maxpos){
			$current = window[el];
			if($current+posX <= -maxpos || $current+posX>0){
				return false;	
			}else{
				posX = posX + $current;
				
				window[el] = posX;
				
			}

		}

	
		if(adder==true){$side_tween = true;}
		$(el).set('tween', {
				onComplete: function(e) { $side_tween = false;},

				duration: tweentime
			}).tween('left', posX+'px');


}


function magicScroll(el,el_navi,direction,override_values){
	//if($vert_tween){return false;}

	

	
	default_values = {
		'navi_opacity' : '1',
		'speed' : '500'	,
		'reset_on_end' : false
	}

	
	if(override_values){
		$extend(default_values, override_values);
	}

	
	if(direction=="down" || direction=="up"){
		$holderSize = $(el).getStyle('height').toInt();
		$moveType = "top";
	}else{
		
		$holderSize = $(el).getStyle('width').toInt();
		$moveType = "left";
	}

	if($(el).getChildren()[0]){
		if(direction=="down" || direction=="up"){
			$sectionSize = $(el).getChildren()[0].getStyle('height').toInt();
		}else{
			$sectionSize = $(el).getChildren()[0].getStyle('width').toInt();
		}
	
	}else{
		return false;	
	}

	if($holderSize>=$sectionSize){
		return false;	
	}
	

	$curPos = window[el];
	

	
	if(direction=="down" || direction=="right"){
		
		//alert($sectionSize + $curPos - $holderSize);
		
		$newPos = $curPos-$holderSize;
		
		if($sectionSize + $curPos - $holderSize <= 0){
			
			if(!default_values['reset_on_end']){
				return false;
			}else{
				$newPos = 0;	
				
				$(el_navi).getChildren()[1].set('tween', {
				duration: default_values['speed']
				}).tween('opacity', [default_values['navi_opacity'],0.2]);
				
				$(el_navi).getChildren()[0].set('tween', {
				duration: default_values['speed']
				}).tween('opacity', 0.2, [default_values['navi_opacity']]);
			}
			
		}
		
		
		

		$(el).getChildren()[0].set('tween', {
					duration: default_values['speed']
				}).tween($moveType, $newPos+'px');
		window[el] =  $newPos;

		if($curPos==0){
			$(el_navi).getChildren()[1].set('tween', {
				duration: default_values['speed']
				}).tween('opacity', [0.2,default_values['navi_opacity']]);
		}
		
		if($sectionSize + $newPos - $holderSize <= 0){
			// disable next button;
			$(el_navi).getChildren()[0].set('tween', {
				duration: default_values['speed']
				}).tween('opacity', [default_values['navi_opacity'],0.2]);
		}

	}else{
		
		if($curPos>=0){
			return false;
		}
	
		
		$newPos = $curPos+$holderSize;
	
		if($newPos>0){
			$newPos = 0;	
		}
		

		$(el).getChildren()[0].set('tween', {
					duration: default_values['speed']
				}).tween($moveType, $newPos+'px');
		
		window[el] =  $newPos;
	
	
	
		if($(el_navi).getChildren()[0].getStyle('opacity')<=0.3){
			$(el_navi).getChildren()[0].set('tween', {
						duration: default_values['speed']
						}).tween('opacity', [0.2,default_values['navi_opacity']]);
		}
	
		if($newPos==0){
			// disable back button;
			$(el_navi).getChildren()[1].set('tween', {
				duration: default_values['speed']
				}).tween('opacity', [default_values['navi_opacity'],0.2]);
		}
	
	
	}
	
	$(el).getElements("span.fadein").set('tween', {
				onComplete: function(el2) {},
				duration: default_values['speed']*2
				}).tween('opacity', 0,1);

	
}


function nw_click(mylink) {
		window.open(mylink,'sharer','toolbar=0,status=0,width=626,height=436'); 
		return false;
	}

function switchFade(){
	// currently old will work for one set of fades, can expand to multiple in future
	var found = false;
	var next = false;
	var first = false;
	$$('.fader').each(function(el){
		if(!first){first=el;}
		if(found && !next){next = el;}
		if(el.hasClass('selected')){found = el;}
	});
	
	if(!next){next=first;}
	SwitchaRoo(found,next);
}

function SwitchaRoo(el,el2){
	/*

	*/
	el.removeClass('selected');
	el2.set('tween', {
				comlpete: function(){alert(1);},
				duration: 1000
				}).tween('opacity', [0,1]);
	el2.addClass('selected');
	FadeOut.delay(1000,null,el);
}

function FadeOut(el){
	el.set('tween', {
			duration: 1000
			}).tween('opacity', [1,0]);	
}


function openWindow(el,myw,myh,mylink,mytitle) {
	

	if(!myw){myw=850;}
	if(!myh){myh=700;}
	if(Browser.Engine.trident && !mylink){
		newwindow=window.open(mylink,mytitle,'height='+myh+',width='+myw);
		if (window.focus) {newwindow.focus()}
		/*
		Shadowbox.open({
			content:    el,
			player:   "iframe",
			height:     myh,
			width:      myw,
			options : { handleOversize: "drag"}
		});
		*/
		return false;
	}else{
		if(mylink){
			mylink = mylink;
		}else{
			mylink = el.get('href');
		}
		
		if(mytitle){
			mytitle = mytitle;
		}else{
			mytitle = el.get('title');
		}
	}

	/*
	Shadowbox.open({
			content:    mylink,
			player:   "iframe",
			title:      mytitle,
			height:     myh,
			width:      myw,
			options : { handleOversize: "drag"}
		});
	*/
	newwindow=window.open(mylink,mytitle,'height='+myh+',width='+myw);
	if (window.focus) {newwindow.focus()}
	return false;	
}	


