// set globals in common.js

//var g = {}; g.timerId =0;g.ps=[];
//function loadEvents(){
//	for(var i=0; i<g.ps.length; i++){
//		g.ps[i].timer = setTimeout('g.ps['+i+'].obj.next()',g.ps[i].interval);
//	}
//}

jQuery.fn.infinateSlider = function(opts){
	var t = jQuery(this);
	
	if (!t.length){return false;}
	
	var defaults = {
		scroller: '#scroller',
		mask: 'div.mask',
		btnNext: '#sliderNext',
		btnPrev: '#sliderPrev',
		items: 'li',
		speed: 600,
		paging: true,
		controls: '#sliderControls',
		infoSpeed: 600,
		interval: 6000,
		prefix: 'ps_',
		auto: true,
		caption: true,
		captionSpeed: 200
	}
	var o = jQuery.extend({},defaults,opts);
	
	var scroller = t.find(o.scroller);
	var panels = t.find(o.items);
	var total = panels.length;
	var panelW = panels.width();
	var btnNext = t.find(o.btnNext);
	var btnPrev = t.find(o.btnPrev);
	var btns = btnNext.add(btnPrev);
	var controls = jQuery(o.controls);
	
	if(panels.length <=1){btns.hide();return false;}
	if(o.caption){ var caption = jQuery('#caption'); }
	
	var current = 1;
	var x = g.timerId;
	var stopTimer = false;
	
	var fadeSpeed = 200;
	
	var methods = {
		//x: g.timerId,
		next: function(){
			if( scroller.is(':animated') ){return};
			if(o.auto){ methods.clearTimer(); }
			methods.setCurrent();
			current++;
			o.caption ? methods.caption() : methods.move(); 
			return false;
		},
		move: function(){
			scroller.animate({left:'-='+panelW},o.speed,function(){methods.panelAniEnd()});
		},
		prev: function(){
			if( scroller.is(':animated') ){return false};
			scroller.find(o.items).last().remove().prependTo(scroller);
			methods.setCurrent();
			scroller.css({left:-panelW}).animate({left:'+='+panelW},o.speed,function(){current--;});
			return false;
		},
		init: function(){
			scroller.css({width:panelW*total})
			panels
				.css({float:'left'})
				.each(function(i){ jQuery(this).attr('id',o.prefix+(i+1)) });
			btnNext.click(methods.next);
			btnPrev.click(methods.prev);
			methods.initPaging();
			if(o.caption){ methods.initCaption(); }
			
		},
		initPaging: function(){
			if(!o.paging) {return false;}
			var controls = jQuery(o.controls);
			var pagingBtns = '<ul id="paging">'
			panels.each(function(i){
				var n =i+1;
				pagingBtns +='<li><a id="paging_'+n+'" href="javascript:g.ps['+x+'].obj.goTo('+n+');"><span>'+n+'</span></a></li>';
			});
			pagingBtns+='</ul>';
			controls.prepend(pagingBtns).find('a:first').addClass('active');
		},
		panelAniEnd: function(){
			scroller.find(o.items+':first').remove().appendTo(scroller).end()
			scroller.css({left:0});
			if(o.auto){this.setTimer(x)}
			if(o.caption){controls.animate({bottom:0},o.captionSpeed)};
			if(o.paging){methods.updatePaging()}			
		},
		initTimer: function(){
			g.ps[g.timerId] = {};
			g.ps[g.timerId].obj = this;
			g.ps[g.timerId].interval = o.interval;
			g.timerId ++;
			btns.hide();
			t.bind({
				mouseenter:function(){methods.clearTimer(); stopTimer = true;methods.fadeBtns('show');},
				mouseleave: function(){stopTimer = false;methods.setTimer(x);methods.fadeBtns('hide');} 
			})
		},
		clearTimer: function(){
			clearTimeout( g.ps[x].timer);
		},
		fadeBtns: function(state){
			if(state === 'show') { btns.stop(true,true).fadeIn(fadeSpeed); 
			} else if( state === 'hide' ) { btns.stop(true,true).fadeOut(fadeSpeed); }
		},
		setTimer: function(x){
			if(stopTimer){ return false }
			g.ps[x].timer = setTimeout('g.ps['+x+'].obj.next('+x+')',o.interval);
		},
		setCurrent: function(){
			if(current > total-1){current = 0}
			return current;
		},
		goTo: function(n){
			if(n == current){return;}
			var itemCalled = jQuery('#'+o.prefix+n);
			var chain = scroller.find(o.items+':first');
			chain.remove().add(itemCalled).prependTo(scroller);
			current = n;
			methods.next();
		},
		updatePaging: function(){
			jQuery('#paging').find('a.active').removeClass('active');
			jQuery('#paging_'+current).addClass('active');
		},
		initCaption: function(){
			var captionTxt = jQuery('#ps_'+current).find('div.caption').html();
			caption.html(captionTxt);	
		},
		caption: function(){
			var captionTxt = jQuery('#ps_'+current).find('div.caption').html();
			var captionH = caption.outerHeight();
			controls.animate({bottom:-captionH},o.captionSpeed,function(){
				caption.html(captionTxt);
				methods.move();
			});
			//console.log(captionTxt);
		}
	}
	if(o.auto){ methods.initTimer(); }
	methods.init();
	
	return methods;
}

$.fn.overlay = function(opts){
	return this.each(function(){
	
	// set defaults 
	var defaults = {
		bgcolor: '#000',
		opacity: '0.8',
		href: $(this).attr('href'),
		speed: 400
	}
	var o = $.extend({},defaults,opts);
	
	var ovGL = {a:jQuery(this)};
	
	var methods = {
		init: function(){
			if(!jQuery('#ov').length) {
				var markup = '<div id="ovFade"></div><div id="ovWrap"><div id="ov"><div id="ovLoad"></div></div></div>';
				jQuery('body').prepend(markup);
				ovGL.ov = jQuery('#ov');
				ovGL.ovLoad = jQuery('#ovLoad');
				ovGL.ovWrap = jQuery('#ovWrap');
				ovGL.ovFade = jQuery('#ovFade');
				
				jQuery(ovGL.ovFade,ovGL.ov,ovGL.ovWrap).hide();
				
				// set styles -------------
				ovGL.ovFade
					.css({height:jQuery(document).height(),background:o.bgcolor,opacity:o.opacity,position:'absolute',width:'100%',zIndex:'999',left:0,top:0})
				ovGL.ovWrap.css({position:'absolute',width:'100%',zIndex:'999',height:0}).hide();
				ovGL.ov.css({margin:'auto',position:'relative'})
				
				//bind Events -------------
				ovGL.a.click(this.openOv);
				ovGL.ovFade.click(this.closeOv);
			}
			
		},
		openOv: function(){
			ovGL.ovFade.fadeIn(o.speed,function(){ methods.loadContent() });	
		},
		closeOv: function(){
			ovGL.ovWrap.hide();
			ovGL.ovFade.fadeOut(o.speed);
		},
		loadContent: function(){
			var img = new Image();
			var $img = jQuery(img);
			img.onload = function(){
				ol = ovGL.ovLoad; 
				ol.html(img).show();
				ovGL.ovWrap.show();
				var mt = (jQuery(window).scrollTop() + (window.innerHeight/2)) - (jQuery(img).height()/2);
				ovGL.ov.css({ width:jQuery(img).width(),top:mt,left:'50%',marginLeft:-$img.width()/2});
			}
			img.src = o.href;
//			ovGL.ovLoad.load(o.href,function(){
//				ovGL.ovLoad.show();	
//			})
		}
		
	
	}
	
	methods.init();
	
	// add click event
	$(this).click(function(){
		//addOverlay();
		return false;
	})
	function addOverlay(){
		var ovFade = '<div id="ovFade"></div>';
		var ovWrap = '<div id="ovWrap"></div>';
		$('body').prepend(ovWrap).prepend(ovFade).addClass('hasOverlay');
		$('#ovFade').css({
			background: o.bgcolor,
			opacity: o.opacity,
			width: '100%',
			height: $(document).height(),
			position: 'absolute',
			top: '0',
			left: '0',
			zIndex: '999'
		})
		.hide()
		.fadeIn('',addOvContent)
		.click(function(){
			$('#ovFade,#ovWrap').remove();
			$('body').removeClass('hasOverlay');
		});// #ovFade
		
	} // addOverlay 
	
	function addOvContent(){
		var ov = '<div id="ov"></div>';
		$('#ovWrap').append(ov);
		contentClone = $(''+o.contentPath).clone(true).attr('id','cloned');
		
		$('#ov')
		.append(contentClone)
		.prepend('<a class="ov_close" href="#" onclick="$(\'#ovFade,#ovWrap\').remove();$(\'body\').removeClass(\'hasOverlay\');return false;"></a>')
		.css({
			position: 'absolute',
			zIndex: '999',
			width: $('#cloned').outerWidth(),
			left: '50%',
			top: ($(window).height()/2)+($(window).scrollTop())+'px',
			marginLeft: '-'+$('#cloned').outerWidth()/2+'px',
			marginTop: '-'+$('#cloned').outerHeight()/2+'px'
		})
	}
	
	});
} // end fn.overlay

