// marquee 连续滑动
(function($){
	var opt = {type:'left',interval:30,step:1};
	$.fn.marquee = function(o){
		opt = $.extend(opt, o || {});
		return this.each(function(){new imarquee($(this));});
	};
	var imarquee = function(obj){
		var me = this;
		obj.parent().css({'position':'relative','overflow':'hidden'});
		obj.css({'position':'absolute','left':'0','top':'0','margin':'0'});
		me.element = obj;
		me.playing = false;
		if(obj.find('img').length>0){
			//有图片的话等待图片载入完毕
			me.imgnums = obj.find('img').length;
			obj.find('img').load(function(){me.ckimg();});
            me.ckimg();
			//超时检查是否已经启动，如果没有则启动
			//setTimeout(function(){if(!me.playing) me.start();},8000);
		}else{
			//没有图片直接开始
			me.start();
		}
	};
	imarquee.prototype = {
        ckimg:function(){
            var me = this;
            me.loaded = 0;
            me.element.find('img').each(function(){if($(this).width()>0) me.loaded ++;});
            if(me.loaded==me.imgnums && !me.playing) me.start();
        },
		start:function(){
			var me= this;
			me.max = opt.type=='left' ? me.element.width() : me.element.height();
			me.element.append(me.element.find('> *').clone());
			
			me.stamp = 0;
			me.handle = setInterval(function(){me.play();},opt.interval);
			me.element.mouseover(function(){clearInterval(me.handle);});
			me.element.mouseout(function(){me.handle = setInterval(function(){me.play();},opt.interval);});
			me.playing = true;
		},
		play:function(){
			var me = this;
        
			if(me.stamp <= -me.max) me.stamp = 0;
			me.element.css(opt.type == 'left' ? 'left' : 'top',me.stamp+'px');
			me.stamp -= opt.step;

            if(opt.debug) $('#debug').html(me.stamp+'|'+me.max);
		}
	};
})(jQuery);
// imgslide 图片轮播
(function($){
	$.fn.imgslide = function(o){
		var opt = {type:'slide',titlebar:null,urls:[],imgs:[],titles:[],navType:0,bar:false,interval:4,speed:300};
		opt = $.extend(opt, o || {});
		opt.hasLink = opt.urls.length > 0;
		opt.hasTitle = opt.titles.length > 0;
		return this.each(function(){this.slide = new imgslide($(this),opt);});
	};
	var imgslide = function (obj,opt){
		//检查必备参数
		if(opt.imgs.length<1) return;
		
		var me = this;
		
		this.opt = opt;
		this.imgnums = opt.imgs.length;
		this.container = obj;
		this.curr = -1;
        this.playing = false;
		this.titlebar = null;
		this.nav = $('<div>').addClass('imgnav');
		
		//定义外观
		obj.css({position:'relative',overflow:'hidden'});
		this.width = obj.width();
		this.height = obj.height();
		//已载入图片计数
		this.loaded = 0;
		this.loading();
		//添加容器
		this.obj = $('<div>').css({
			height:this.height,
			width:this.width,
			overflow:'hidden'
		}).appendTo(obj);
		
		
		//填充轮播图片及导航
		for(var i=0;i<this.imgnums;i++){
			var wraps = opt.hasLink ? '<a href="'+opt.urls[i]+'"></a>' : '';
			//填充轮播图片
			$('<img>').load(function(){
				me.loaded ++;
				me.loadTo(Math.ceil(me.loaded/me.imgnums*100)+'%');
				if(me.loaded==me.imgnums){
					me.addNav();
					me.hideLoad();
					me.next();
					me.start();
				}
			})
            .attr('src',opt.imgs[i])
			.css({width:this.width,height:this.height,margin:0,padding:0})
			.appendTo(this.obj)
			.wrap(wraps);
		}
		//fade模式处理
		if(opt.type=='fade') me.obj.find('img').css({left:0,top:0,position:'absolute',display:'none'});
        return this;
	};
	imgslide.prototype = {
		start:function(){
			var me = this;
            me.playing = true;
			me.handle = setInterval(function(){me.next();},me.opt.interval*1000);
		},
		pause:function(){
			var me = this;
            me.playing = false;
			clearInterval(me.handle);
		},
		next:function(){
			var me = this;
			var next =  (me.curr < me.opt.imgs.length - 1 ) ? me.curr + 1 : 0;
			me.to(next);
		},
		to:function(x){
			var me = this;
			if(me.curr==x ) return;
			
			if(me.opt.type=='fade'){
				//fade模式
				me.obj.find('img:eq('+me.curr+')').fadeOut('slow');
				me.obj.find('img:eq('+x+')').fadeIn('slow');
			}else{
				//slide模式
				me.obj.animate({scrollTop:me.height*x},me.opt.speed);
			}
			
			if(this.titlebar) this.titlebar.html(me.opt.titles[x]);

			me.curr = x;
			if(this.opt.navType==2 && this.playing){
                //移动标题栏使之可视
                if( x >= this.opt.titlenums) this.titnav.css({'left':'auto','right':0});
                else this.titnav.css({'right':'auto','left':0});
            }
			this.nav.find('.slidenavs').removeClass('hover');
            this.nav.find('.slidenavs:eq('+x+')').addClass('hover');
            if(!this.opt.css){
                this.nav.find('.slidenavs').css('opacity',0.6);
                this.nav.find('.slidenavs:eq('+x+')').css('opacity',1);
            }
		},
		addNav:function(){
            var me=this;
            this.barheight = me.opt.navType == 1 ? 70 : 30;

            if (me.opt.hasTitle && me.opt.navType!=2){
                this.titlebar = me.opt.titlebar ? $(me.opt.titlebar) : $('<div>').addClass('slidetitlebar').css({
                    position:'absolute',
                    width:this.width,
                    height:this.barheight,
                    overflow:'hidden',
                    'line-height':this.barheight+'px',
                    'text-indent':'10px',
                    left:0,
                    bottom:0,
                    'z-index':1010
                }).appendTo(this.container);

            }
			if(this.opt.navType==-1) return true;
			
			//添加半透明层衬托导航 [因为透明度继承问题暂时如此解决]
			if(me.opt.bar){
				this.barbg = $('<div>').addClass('slidebarbg').css({
					position:'absolute',
					width:this.width,
					height:this.barheight,
					left:0,
					bottom:0,
					'z-index':1000
				}).appendTo(this.container);

                if(!this.opt.css) this.barbg.css({opacity:0.4,'background-color':'#000000'});
			}
			
			//添加导航容器
			this.nav.css({
				position:'absolute',
				width:this.width,
				height:this.barheight,
				left:0,
				bottom:0,
				'z-index':1100
			}).appendTo(this.container);

            if(me.opt.navType==2){
                if(!this.opt.titlenums) this.opt.titlenums = 4
                if(this.opt.titlenums < this.imgnums/2) this.opt.titlenums = Math.ceil(this.imgnums/2);
                this.titlewidth = Math.floor((this.width - 32)/this.opt.titlenums);

                //底部标题导航按钮
                this.titnavpre = $('<em>').click(function(){me.to(0);}).addClass('titnavpre').css({
                    'float':'left',
                    'display':'block',
                    'width':15,
                    'margin-right':'1px',
                    'height':this.barheight
                });
                this.titnavnext = $('<em>').click(function(){me.to(me.imgnums-1);}).addClass('titnavnext').css({
                    'float':'left',
                    'display':'block',
                    'width':15,
                    'height':this.barheight
                });
                this.titnavbody = $('<p>').css({
                    'width':this.opt.titlenums*this.titlewidth,
                    'position':'relative',
                    'float':'left',
                    'margin-right':'1px',
                    'overflow':'hidden',
                    'height':this.barheight
                });
                
                this.titnav = $('<p>').css({
                    'position':'absolute',
                    'left':0,
                    'top':0,
                    'height':this.barheight,
                    'width':this.titlewidth*this.imgnums
                }).appendTo(this.titnavbody);

                this.nav.append(this.titnavpre).append(this.titnavbody).append(this.titnavnext);
                
            } 
			
			//填充导航
			for(var i=0;i<this.imgnums;i++){
				//填充导航
                if(me.opt.navType==2 && me.opt.hasTitle){//标题横排模式
                    var _x = $('<span>').html(me.opt.titles[i]).css({
                        display:'block',
                        width:this.titlewidth-1,
                        'margin-right':'1px',
                        'float':'left',
                        'overflow':'hidden',
                        'height':this.barheight,
                        'line-height':this.barheight+'px'
                        
                    }).addClass('slidenavs').appendTo(this.titnav);

                    if(!this.opt.css) _x.css({'background':'#000','color':'#fff','text-align':'center'});
                    
                } else if(me.opt.navType==1){
					var _x = $('<img>').attr('src',me.opt.imgs[i]).addClass('slidenavs').css({					
						height:45,
						width:'auto',
						margin:'10px 10px 0 0'
					}).appendTo(this.nav.css({
						'text-align':'center'
					}));
                    if(!this.opt.css) _x.css({opacity:0.5,border:'#ffffff 2px solid',padding:'1px'});
				} else {
					//数字样式
					var _x = $('<span>').html(i+1).addClass('slidenavs').css({
						
						'margin-right':'8px',
						'font-size':'12px'
					}).appendTo(this.nav.css({
						height:this.barheight-6,
						'text-align':'right'
					}));
                    if(!this.opt.css) _x.css({padding:'1px 3px',border:'#fff 1px solid',color:'#fff',background:'#000',opacity:0.5});
				}
			}
			this.nav.find('.slidenavs').each(function(x,e){
				$(e).mouseover(function(){me.pause();me.to(x);}).mouseout(function(){me.start();});
			});
		},
		loading:function(){
			this.loadDiv = $('<div>').css({
				position:'absolute',
				width:100,
				opacity:0.6,
				height:16,
				border:'#222 1px solid',
				background:'#ffffff',
				padding:'1px',
				'font-size':'12px',
				top:Math.ceil(this.height/2)-16,
				left:Math.ceil(this.width/2)-50,
				'z-index':3000,
				color:'#333'
			}).appendTo(this.container);
			this.loadbar = $('<div>').css({
				width:0,
				height:'16px',
				background:'#369'
			}).appendTo(this.loadDiv);
		},
		loadTo:function(w){
			this.loadbar.css('width',w);
		},
		hideLoad:function(){
			this.loadDiv.hide();
            if(this.opt.loaded) eval(this.opt.loaded + '(this);');
		}
	};
})(jQuery);

// slideshow 变换轮播
(function($){
	var opt = {type:'fade',interval:3};
	$.fn.slideshow = function(o){
		opt = $.extend(opt, o || {});
		return this.each(function(){new islideshow($(this));});
	};
	var islideshow = function(obj){
		var me = this;
		me.elsc = obj.find('> .elsc');
		me.elsc.css({'position':'relative'});
		me.els = me.elsc.find('> a');
		me.els.css({'position':'absolute','display':'none'});
		me.titlebar = obj.find('> .title');
		me.numc = obj.find('> .numnav');
		me.titlec = obj.find('> .titlec');
		me.navs = [];
		if(me.numc.length>0){
			me.els.each(function(x){
				me.navs[x] = $('<span>'+(x+1)+'</span>').appendTo(me.numc).mouseover(function(){me.pause();me.to(x);}).mouseout(function(){me.start();});
			});
		}
		if(me.titlec.length>0){
			me.els.each(function(x){
				me.navs[x] = $('<li>'+$(me.els.get(x)).find('img').attr('alt')+'</li>').appendTo(me.titlec).mouseover(function(){me.pause();me.to(x);}).mouseout(function(){me.start();});
			});
		}
		me.curr = -1;
		me.next();
		me.start();
	};
	islideshow.prototype = {
		start:function(){
			var me = this;
			me.handle = setInterval(function(){me.next();},opt.interval*1000);
		},
		pause:function(){
			var me = this;
			clearInterval(me.handle);
		},
		next:function(){
			var me = this;
			var next =  (me.curr < me.els.length - 1 ) ? me.curr + 1 : 0;
			me.to(next);
		},
		to:function(x){
			var me = this;
			var ocurr = me.curr;
			me.curr = x;
			if(me.navs[ocurr]) me.navs[ocurr].removeClass('hover');
			if(ocurr>0) $(me.els.get(ocurr)).hide();
			if(me.navs[me.curr]) me.navs[me.curr].addClass('hover');
			$(me.els.get(me.curr)).show();
			if(me.titlebar.length>0) me.titlebar.html($(me.els.get(ocurr)).find('img').attr('alt'));
		}
	};
})(jQuery);
//jQuery Plugin: Drop Shadow Text
// call like this: $(element).textDropShadow();
(function($) {
 $.fn.textDropShadow = function(){
	 return this.each(function(){
		 $(this).html('<span class="jq-shadow">'+$(this).html()+'</span><span class="jq-o">'+$(this).html()+'</span>');
	 });
 };
})(jQuery);
(function($){
    $.fn.maxWidth = function(mwidth){
        return this.each(function(){
            if($(this).width() > mwidth) $(this).width(mwidth);
            $(this).load(function(){if($(this).width() > mwidth) $(this).width(mwidth);});
        });
    };
})(jQuery);
(function($){
	var blank = new Image();
	$.fixPng = function(){
		if($.browser.msie && $.browser.version == "6.0"){
            document.execCommand("BackgroundImageCache" , false, true);  
            blank.src = 'images/blank.gif';
			$('img[src$=".png"]').each(function() {
		       if (!this.complete) {
		         this.onload = function() { fixPng(this) };
		       } else {
		         fixPng(this);
		       }
			});
		}
	};
	function fixPng(png) {
	   // get src
	   var src = png.src;
	   // set width and height
	   if (!png.style.width) { png.style.width = $(png).width(); }
	   if (!png.style.height) { png.style.height = $(png).height(); }
	   // replace by blank image
	   png.onload = function() { };
	   png.src = blank.src;
	   // set filter (display original image)
	   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
})(jQuery);
(function($){
$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
})(jQuery);
