(function( $ )
{

	if ( typeof (Array.prototype.indexOf) == 'undefined' ) {
		Array.prototype.indexOf = function(item) {
			for(var i = 0; i < this.length; i++ ) {if ( this[i] == item )return i;}
			return -1;
		}
	}
	String.prototype.splitUrls = function() {
	    var urls = this.split(',http');
		for(var i = 0; i < urls.length; i++ ) {if ( i > 0 ) urls[i] = 'http' + urls[i];}
		return urls;
	}
	var debug = false;
	jQuery.fn.log = function (msg) {
		if ( debug && console )console.log("%s: %o", msg, this);
		return this;
    };
 
	jQuery.fn.extend({
		executeOnce: function(label,delay, fn, option) {return this.each(function() {jQuery.timer.delayExecute(this, delay, label, fn, option);});},
		stopExecution: function(label) {return this.each(function() {jQuery.timer.remove(this, label);});}
	});
	
	jQuery.extend({
		timer: {
			delayExecute: function(element, delay, label, fn, option) {
				var timer = jQuery.data(element, label);
				if ( timer ) {
					if ( timer.timerId ) {
						if ( option == 'block' ) return;
						window.clearTimeout(timer.timerId);
						timer.timerId = null;
					}
				} else {
					timer = {};
					jQuery.data(element, label, timer);
				}
				var timerId;
				var handler = function() {
					if ( timer.timerId == timerId ) {
						timer.timerId = null;
						fn.call(element);
					} else {
					}
				};
				timerId = timer.timerId = window.setTimeout(handler,delay);
			
			},
			remove: function(element, label) {
				var timer = jQuery.data(element, label);
				if ( timer ) {
					if ( timer.timerId ) {
						window.clearTimeout(timer.timerId);
						timer.timerId = null;
					}
				}
			}
		}
	});


	$.fn.document = function() {
		var element = this[0];
 		if ( element.nodeName.toLowerCase() == 'iframe' )
 			return element.contentWindow.document;
		else
			return $(this);
	};
	
  	var getTransparent = function(el){
  		for(var n=0;n<el.parents().length;n++){
			var parent = el.parents().get(n);
			var pBg = $.browser.safari ? $(parent).css("background") : $(parent).css("background-color");
			if(pBg!='' && pBg!='transparent'){
				return pBg;
			}
		}
		return '#FFF';
  	}

	$.fn.hoverClass = function(clsName) {
		this.bind('mouseover',function(e){$(this).addClass(clsName);}).bind('mouseout',function(e){$(this).removeClass(clsName);});
        return this;
	};
	
	$.fn.loadContent = function(url,data,callback,errorCallback ) {
		var parent = this;
		$.ajax({
			type: "GET",
			url: url,
			//data: $("#flip").serialize(),
			beforeSend: function(){
			},
			success: function(html){
				$(html).appendTo(parent).find('input:text').inputFields();
				callback(html);
			},
		    error: errorCallback
		});
	};
	
	var fadeFx = function( opts) {
		opts.showAnim	   = { opacity: 1 };
		opts.hideAnim   = { opacity: 0 };
		opts.cssBeforeShow = { opacity: 0,display:''};
		opts.cssAfterShow = { opacity:''};

		opts.hideSpeed = 100;
	    opts.showSpeed = 1200;
		opts.sync=true;
		opts.beforeHide.push(function(toHide){
		    var offset = toHide.position();
			this.cssBeforeHide = {top:offset.top + 'px',left:offset.left + 'px',opacity:1,zIndex:100000,position:'absolute'}; 
			this.cssAfterHide = {position:'',top:'',left:'',opacity:'',zIndex : '',display:'none'};
		})
		if ($.browser.msie) { 
			opts.beforeShow.push(function(toShow){
		    if ( this.dynamicLoad && toShow[0].tagName == 'IMG' && this.center ) {
		        var h = toShow.outerHeight();
		        var ph = toShow.parent().outerHeight();
				var m = (ph-h)/2;
				if ( m > 0 )
                	toShow.css('marginTop', m + 'px');
		    }
			});
		}
	};

	var vScrollFx = function( opts) {
		opts.hideAnim   = { height: 'hide' };
		opts.showAnim = { height: 'show' };
		opts.cssAfterShow = { display: '',top:'0px'}
		opts.hideSpeed = 1300;
	    opts.showSpeed = 1300;
	};

	var tossFx = function(opts) {
		if (opts.xOffset == 0 )
			opts.xOffset = 100;
		if ( opts.yOffset == 0 )
			opts.yOffset = 50;
		opts.beforeHide.push(function(toHide){
		    var offset = toHide.position();
			var color = getTransparent(toHide);
			this.cssBeforeHide = {top:offset.top + 'px',left:offset.left + 'px',opacity:1,zIndex:100000,backgroundColor:color,position:'absolute'};
			
			this.cssAfterHide = {position:'',top:0,left:0,opacity:'',zIndex : '',display:'none'};
			this.cssBeforeShow = {opacity:0,zInex:990000,display:''};//,verticalAlign: 'middle'};//top:'',left:'',position:'static'};
			this.cssAfterShow = {opacity:'',zIndex:''};
			this.hideAnim = {left: (offset.left + this.xOffset) + 'px', top: (offset.top - this.yOffset) + 'px',opacity:0};

		});
		if ( this.removeAfterHide ) {
			opts.afterHide.push(function(toHide){toHide.remove();});
		}
		if ($.browser.msie) { 
			opts.beforeShow.push(function(toShow){
		    	if ( this.dynamicLoad && toShow[0].tagName == 'IMG' && this.center ) {
		        	var h = toShow.outerHeight();
		        	var ph = toShow.parent().outerHeight();
					var m = (ph-h)/2;
					if ( m > 0 ) toShow.css('marginTop', m + 'px');
		    	}
			});
        }
		opts.showAnim = { opacity:1 };
		opts.sync = true;
		opts.hideSpeed = 1200;
		opts.showSpeed = 500;
	};

	var zoomFx = function(opts) {
		opts.beforeHide.push(function(toHide){
			var pw = toHide.parent().outerWidth();
			var ph = toHide.parent().outerHeight();
			var w = toHide.outerWidth();
			var h = toHide.outerHeight();
			var x = (pw-w)/2;
			var y = (ph-h)/2;
		    var offset = toHide.position();
			var color = getTransparent(toHide);
			this.cssBeforeHide = {top:offset.top + 'px',left:offset.left + 'px',width:w + 'px',height: h + 'px',opacity:1,zIndex:100000,backgroundColor:color,position:'absolute'};
			this.jqContentContainer.css('display','block');
			offset = this.jqHideTarget.position();

			this.hideAnim={width:this.jqHideTarget.outerWidth() + 'px',height:this.jqHideTarget.outerHeight() + 'px',top:offset.top + 'px',left: offset.left + 'px'};

		});
		if ( this.removeAfterHide ) {
			opts.afterHide.push(function(toHide){toHide.remove();});
		}
		opts.beforeShow.push(function(toShow){
		    var offset = this.jqShowSrc.position();
			var ws = this.jqShowSrc.outerWidth();
			var hs = this.jqShowSrc.outerHeight();
			var pw = this.jqContentContainer.outerWidth();
			var ph = this.jqContentContainer.outerHeight();
			var w = toShow.outerWidth();
			var h = toShow.outerHeight();
			var x = (pw-w)/2;
			var y = (ph-h)/2;
			var color = getTransparent(toShow);
			this.cssBeforeShow = {top:offset.top + 'px',left:offset.left + 'px',width:ws + 'px',height: hs + 'px',opacity:1,zIndex:100000,backgroundColor:color,position:'absolute'};
			//if ( !$.browser.msie)
				//this.cssAfterShow= {position:'',top:'',left:'',zIndex:'',marginTop:y + 'px',marginLeft:y + 'px'};
			var parent = this.jqContentContainer;
			//alert(parent.outerWidth() + ":" + parent.outerHeight());
			//alert(this.jqContentContainer.offset().left + ":" + this.jqContentContainer.offset().top);
			offset = this.jqContentContainer.position();
			//offset = this.jqContentContainer.parent().position();
			this.showAnim = { width: w + 'px',height: h + 'px',left: (offset.left + x)+ 'px',top: (offset.top + y)+ 'px'};

		});

		opts.sync = true;
		opts.hideSpeed = 200;
		opts.showSpeed = 200;
	};

	var zoomxFx = function(opts) {
		opts.beforeHide.push(function(toHide){
			var pw = toHide.parent().outerWidth();
			var ph = toHide.parent().outerHeight();
			var w = toHide.outerWidth();
			var h = toHide.outerHeight();
			var x = (pw-w)/2;
			var y = (ph-h)/2;
			this.cssBeforeHide = {};
			this.hideAnim={width:'hide'};
			//if ($.browser.msie) { 
		    	//if ( this.dynamicLoad && toHide[0].tagName == 'IMG' && this.center ) {
		    	if ( this.center ) {
		        	var h = toHide.outerHeight();
		        	var ph = toHide.parent().outerHeight();
					var m = (ph-h)/2;
					if ( m > 0 ) toHide.css('marginTop', m + 'px');
		    	}
        	//}

		});
		if ( this.removeAfterHide ) {
			opts.afterHide.push(function(toHide){toHide.remove();});
		}
		opts.beforeShow.push(function(toShow){
			var pw = toShow.parent().outerWidth();
			var ph = toShow.parent().outerHeight();
			var w = toShow.outerWidth();
			var h = toShow.outerHeight();
			var x = (pw-w)/2;
			var y = (ph-h)/2;
			this.cssBeforeShow = {};
			this.showAnim = { width:'show'};
			//if ($.browser.msie) { 
		    	//if ( this.dynamicLoad && toShow[0].tagName == 'IMG' && this.center ) {
		    	if ( this.center ) {
		        	var h = toShow.outerHeight();
		        	var ph = toShow.parent().outerHeight();
					var m = (ph-h)/2;
					if ( m > 0 )
                		toShow.css('marginTop', m + 'px');
		    	}
        	//}

		});

		//opts.sync = true;
		opts.hideSpeed = 800;
		opts.showSpeed = 1200;
	};
	
	var shuffleFx = function(opts) {
		opts.beforeHide.push(function(toHide){
			var w = toHide.parent().outerWidth();
			var offset = toHide.position();
			var offset2 = toHide.parent().position();
			var color = getTransparent(toHide);
			//toHide.css('background-color', color);
			this.cssBeforeHide = {top:offset.top + 'px',left:offset.left + 'px',zIndex:800000,position:'absolute',backgroundColor:color};
			this.cssAfterHide = {top:'',left:''};//,zIndex :'',position:''};//,opacity:''};
			this.hideAnim = {left: '-=' + (w+this.xOffset) + 'px',top:'-=' + this.yOffset + 'px'};//,opacity:.4};
			this.hideAnim2 = {left: '+=' + (w+this.xOffset) + 'px',top:'+=' + this.yOffset + 'px'};//,opacity:0};

			this.cssBeforeShow = {zIndex:990000,opacity:0,position:'absolute',backgroundColor:color};
			this.cssAfterShow = {zIndex: ''};
			this.showAnim = {opacity:1};

		});
		opts.doHideAnimate = function(jqToHide,jqToShow) {
			var mgr = this;
			jqToHide.animate(this.hideAnim, this.hideSpeed).animate(mgr.hideAnim2, mgr.hideSpeed, function() { //
					jqToHide.hide();
					mgr.doAfterHide(jqToHide);
            });
			this.doShow(jqToShow);
		}

		opts.beforeShow.push(function(toShow){
			
			var pw = toShow.parent().outerWidth();
			var ph = toShow.parent().outerHeight();
			var w = toShow.outerWidth();
			var h = toShow.outerHeight();
			var x = (pw-w)/2;
			var y = (ph-h)/2;
			this.cssBeforeShow = {};
			if ($.browser.msie) { 
		    	if ( this.center ) {
		        	var h = toShow.outerHeight();
		        	var ph = toShow.parent().outerHeight();
					var m = (ph-h)/2;
					if ( m > 0 )
                		toShow.css('marginTop', m + 'px');
		    	}
        	}

		});

		opts.sync = true;
		opts.hideSpeed = 500;
		opts.showSpeed = 100;

	};
	var replaceImage = function (jqToHide,jqToShow) {
		this.doBeforeHide(jqToHide);
		jqToHide.attr('src',jqToShow.attr('src'));
		this.doAfterShow(jqToShow);
	}
	var replaceContent = function (jqToHide,jqToShow) {
		this.doBeforeHide(jqToHide);
		jqToHide.hide();
		this.doAfterHide(jqToHide);
		jqToShow.show();			  
		this.doAfterShow(jqToShow);

	}
		
	var defaultDisplayMgr = {
	    fx:'',
		hideAnim: null,
	    hideSpeed: 0,
		cssBeforeHide : {},
		cssAfterHide : {},
		beforeHide : [],
		afterHide : [],
	    showAnim : null,
	    showSpeed:0,
		cssBeforeShow : {},
		cssAfterShow : {},
		beforeShow : [],
		afterShow : [],
		sync: false,
		removeAfterHide : false,
		xOffset :0,
		yOffset :0,
		doBeforeHide : function(jqToHide) {
			var param = [jqToHide];
			for(i=0; i < this.beforeHide.length; i++ )
				this.beforeHide[i].apply(this,param);
			jqToHide.css(this.cssBeforeHide);
		},
		doAfterHide : function(jqToHide) {
			var param = [jqToHide];
			for(i=0; i < this.afterHide.length; i++ )
				this.afterHide[i].apply(this,param);
			jqToHide.css(this.cssAfterHide);
			jqToHide.hide();
            if (typeof this.onHide == 'function') {
            	this.onHide();
			}
		},
		doBeforeShow : function(jqToShow) {
			var param = [jqToShow];
			for(i=0; i < this.beforeShow.length; i++ )
				this.beforeShow[i].apply(this,param);
			jqToShow.css(this.cssBeforeShow);
		},
		doAfterShow : function(jqToShow) {

			var param = [jqToShow];
			for(i=0; i < this.afterShow.length; i++ )
				this.afterShow[i].apply(this,param);
            jqToShow.css(this.cssAfterShow);
			jqToShow.css('opacity',1).show(); // opacity bit needed to handle reinit case
			if ($.browser.msie) jqToShow[0].style.removeAttribute('filter');
            if (typeof this.onShow == 'function') {
            	this.onShow();
            }
		},
		doHideAnimate : function(jqToHide,jqToShow) {
			var mgr = this;
			jqToShow.hide();
			jqToHide.animate(this.hideAnim, this.hideSpeed, function() { //
				jqToHide.hide();
				mgr.doAfterHide(jqToHide);
				if( !mgr.sync ) mgr.doShow(jqToShow);
            });
			if ( mgr.sync ) this.doShow(jqToShow);
		},
		doShowAnimate : function(jqToShow) {
			var mgr = this;
            jqToShow.animate(this.showAnim, this.showSpeed, function() {
				jqToShow.show();
				mgr.doAfterShow(jqToShow);
             });
		},
		doShow : function (jqToShow) {
			this.doBeforeShow(jqToShow);
 			if ( this.showAnim && this.showSpeed ) {
				this.doShowAnimate(jqToShow);
			} else {
				jqToShow.show();
				this.doAfterShow(jqToShow);
            }
		},
		doHide : function(jqToHide,jqToShow) {
			this.doBeforeHide(jqToHide);
 			if ( this.hideAnim && this.hideSpeed ) {
				this.doHideAnimate(jqToHide,jqToShow);
 			} else {
				jqToHide.hide();
				this.doAfterHide();
			    this.doShow(jqToShow);
	        }
		},
		toggleDisplay : function(jqToHide,jqToShow) {
			this.doHide(jqToHide,jqToShow);
		},
		init : function () {
			switch( this.fx ) {
				case 'toss':
					tossFx(this);
					break;
				case 'zoom':
					zoomFx(this);
					break;
				case 'vScroll':
					vScrollFx(this);
					break;
				case 'fade':
					fadeFx(this);
					break;
				case 'shuffle':
					shuffleFx(this);
					break;
				case 'replaceContent':
					this.toggleDisplay = replaceContent;
					break;
				case 'replaceImage':
					this.toggleDisplay = replaceImage;
					break;
			}

		}
	};
	
	var defaultContentMgr = {
		id : "contentMgr",
		display:$.extend({},defaultDisplayMgr),
		contents:null,
		urlSelect:null,
		activeIndex: 0,
		maxIndex:0,
		wrapIndex : true,
		dynamicLoad : false,
		removeAfter : false,
		isImage : false,
		urlList : null,
		jqContainer: null,
		jqContents : null,
		jqCacheContents : null,
		cache : false,
		slideShowDelay : 500,
		slideShowCycle : 2000,
		slideShowEnabled : false,
		beforeDynamicLoad : function () {
		},
		afterDynamicLoad : function () {
		},
		//initList : [],
		onShow : function () {
//it is possible that hide have not finish
			//this.resetActiveContent();
//do on certian condition only
			this.jqContents = this.jqContainer.find(this.contents);
			this.jqActiveContent = this.jqContents.filter(':visible');

			if ( this.slideShowEnabled )
				this.delayNext();
		},
		onHide : function () {
		},
		delayNext : function (delay) {
			if ( this.slideShowEnabled ) {
				var mgr = this;
				var handler = function () {
					mgr.next();
				}
				this.jqContainer.executeOnce(this.id+'sstimer',delay || this.slideShowCycle, handler, 'block');
			}
		},
		startSlideShow : function() {
			this.slideShowEnabled = true;
			this.delayNext(this.slideShowDelay);
		},
		stopSlideShow : function () {
			this.slideShowEnabled = false;
			this.jqContainer.stopExecution(this.id+'sstimer');
		},
		resetActiveContent : function () {
			var previous = this.jqActiveContent;
			this.jqActiveContent = this.jqContents.filter(':visible');
			if ( this.jqActiveContent.length > 1 ) {
				var idx = this.jqActiveContent.index(previous[0]);
				this.jqActiveContent = $(this.jqActiveContent[idx == 0 ? 1 :0 ]);
			}
		},
		switchToIndex : function(idx) {
			if ( idx != this.activeIndex ) {
				if ( this.cache ) {
					var url = this.urlList[idx];
					if ( this.urlList[url] ) {
						this.switchToContent(this.urlList[url]); 
						this.activeIndex = idx;
					} else {
						this.loadContent(url);
				