/* Desire Theme JavaScript */
function animatemenu() {
    //jQuery(".menu ul").css({display:"none"}); // Fix for opera
    jQuery(".menu ul li").hover(function() {
        jQuery(this).find('ul:first').css({visibility:"visible",display:"none"}).show(400);
    },
    function() {
        jQuery(this).find('ul:first').css({visibility:"hidden"});
    });
}

jQuery(document).ready(function() {
    animatemenu();
});

jQuery(document).ready(function() {
    jQuery('ul li:last-child').addClass('last');
});

function show(elem) {
    jQuery('#' + elem).slideDown('slow');
}

function hide(elem) {
    jQuery('#' + elem).slideUp('slow');
}

function toggle(showelem,hideelem1,hideelem2) {
    document.getElementById(hideelem1 + '-tab').style.backgroundColor = "#fcfcfc";
    document.getElementById(hideelem1 + '-tab').style.borderBottom = "1px #eee solid";
    document.getElementById(hideelem2 + '-tab').style.backgroundColor = "#fcfcfc";
    document.getElementById(hideelem2 + '-tab').style.borderBottom = "1px #eee solid";
    document.getElementById(showelem + '-tab').style.backgroundColor = "#fff";
    document.getElementById(showelem + '-tab').style.borderBottom = "0 none";
    
    hide(hideelem1);
    hide(hideelem2);
    show(showelem);
}

/*go top*/
var scrolltotop={
	//startline: Integer. Number of pixels from top of doc scrollbar is scrolled before showing control
	//scrollto: Keyword (Integer, or "Scroll_to_Element_ID"). How far to scroll document up when control is clicked on (0=top).
	setting: {startline:100, scrollto: 0, scrollduration:1000, fadeduration:[500, 100]},
	controlHTML: '<img src="http://www.yesure.net/up.png" style="width:38px; height:32px" />', //HTML for control, which is auto wrapped in DIV w/ ID="topcontrol"
	controlattrs: {offsetx:50, offsety:50}, //offset of control relative to right/ bottom of window corner
	anchorkeyword: '#top', //Enter href value of HTML anchors on the page that should also act as "Scroll Up" links

	state: {isvisible:false, shouldvisible:false},

	scrollup:function(){
		if (!this.cssfixedsupport) //if control is positioned using JavaScript
			this.$control.css({opacity:0}) //hide control immediately after clicking it
		var dest=isNaN(this.setting.scrollto)? this.setting.scrollto : parseInt(this.setting.scrollto)
		if (typeof dest=="string" && jQuery('#'+dest).length==1) //check element set by string exists
			dest=jQuery('#'+dest).offset().top
		else
			dest=0
		this.$body.animate({scrollTop: dest}, this.setting.scrollduration);
	},

	keepfixed:function(){
		var $window=jQuery(window)
		var controlx=$window.scrollLeft() + $window.width() - this.$control.width() - this.controlattrs.offsetx
		var controly=$window.scrollTop() + $window.height() - this.$control.height() - this.controlattrs.offsety
		this.$control.css({left:controlx+'px', top:controly+'px'})
	},

	togglecontrol:function(){
		var scrolltop=jQuery(window).scrollTop()
		if (!this.cssfixedsupport)
			this.keepfixed()
		this.state.shouldvisible=(scrolltop>=this.setting.startline)? true : false
		if (this.state.shouldvisible && !this.state.isvisible){
			this.$control.stop().animate({opacity:1}, this.setting.fadeduration[0])
			this.state.isvisible=true
		}
		else if (this.state.shouldvisible==false && this.state.isvisible){
			this.$control.stop().animate({opacity:0}, this.setting.fadeduration[1])
			this.state.isvisible=false
		}
	},
	
	init:function(){
		jQuery(document).ready(function($){
			var mainobj=scrolltotop
			var iebrws=document.all
			mainobj.cssfixedsupport=!iebrws || iebrws && document.compatMode=="CSS1Compat" && window.XMLHttpRequest //not IE or IE7+ browsers in standards mode
			mainobj.$body=(window.opera)? (document.compatMode=="CSS1Compat"? $('html') : $('body')) : $('html,body')
			mainobj.$control=$('<div id="topcontrol">'+mainobj.controlHTML+'</div>')
				.css({position:mainobj.cssfixedsupport? 'fixed' : 'absolute', bottom:mainobj.controlattrs.offsety, right:mainobj.controlattrs.offsetx, opacity:0, cursor:'pointer'})
				.attr({title:'Scroll Back to Top'})
				.click(function(){mainobj.scrollup(); return false})
				.appendTo('body')
			if (document.all && !window.XMLHttpRequest && mainobj.$control.text()!='') //loose check for IE6 and below, plus whether control contains any text
				mainobj.$control.css({width:mainobj.$control.width()}) //IE6- seems to require an explicit width on a DIV containing text
			mainobj.togglecontrol()
			$('a[href="' + mainobj.anchorkeyword +'"]').click(function(){
				mainobj.scrollup()
				return false
			})
			$(window).bind('scroll resize', function(e){
				mainobj.togglecontrol()
			})
		})
	}
}

scrolltotop.init()

/*
 * zShowBox 0.4 (图片放大展示)
 * by ZWWoOoOo (http://zww.me/)
 * Copyright (c) 2011 ZWWoOoOo
 * MIT-style license
*/
function zShowBox(domChunk) {
	//为每张图片链接加上 class="zshowbox"
	var zcounter=0;
	$(domChunk+' a').each(function(){
		var a_href = $(this).attr('href').toLowerCase();
		var file_type = a_href.substring(a_href.lastIndexOf('.'));
		if (file_type == '.jpeg' || file_type == '.jpg' || file_type == '.png' || file_type == '.gif' || file_type == '.bmp'){ $(this).addClass('zshowbox').attr('id','zsb-'+zcounter); zcounter++; };
	});
	$(domChunk+' a.zshowbox').click(function(){
		var current=$(this).attr('id').split(/zsb-/)[1],
			pagesize=zsb_getPageSize(),
			zsb_img_url=$(this).attr('href'),
			css_zsb_bg='z-index:9999;overflow:hidden;position:fixed;left:0;top:0;width:100%;height:100%;background:#000 url('+loadingimg+') no-repeat center center;',
			css_zsb='z-index:99999;position:fixed;left:50%;top:50%;',
			css_zsb_img='display:none;border:5px solid #777;border-radius:6px;-moz-border-radius:6px;-webkit-border-radius:6px;box-shadow:1px 1px 5px #333,-1px -1px 5px #333;-moz-box-shadow:1px 1px 5px #333,-1px -1px 5px #333;-webkit-box-shadow:1px 1px 5px #333,-1px -1px 5px #333;',
			css_zsb_p_n='display:none;cursor:pointer;position:absolute;top:50%;line-height:80px;margin:-40px 0 0 0;color:#eee;text-shadow:1px 3px 5px #000;font-size:40px;font-family:Arial,Tahoma;';
		if (typeof document.body.style.maxHeight === "undefined") { //if IE 6
			alert('zShowBox不支持IE6！请你们放过IE6吧，它太老了，就让它安心的去吧……');
			return false;
		} else {
			$('body').append('<div id="zsb_bg" style="'+css_zsb_bg+'"></div><div id="zsb" style="'+css_zsb+'"><img id="zsb_img" style="'+css_zsb_img+'" /><p id="zsb_prev" style="left:-30px;'+css_zsb_p_n+'">&laquo;</p><p id="zsb_next" style="right:-30px;'+css_zsb_p_n+'">&raquo;</p></div>');
			$('#zsb_bg').fadeTo(600,0.7);
			zsh_img('#zsb_img',zsb_img_url,pagesize,current,zcounter);
			$('#zsb_prev,#zsb_next').click(function(){
				if ($(this).attr('id')=='zsb_prev') current--; else current++;
				$(this).parent().prev().css("background-image",'url('+loadingimg+')');
				$('#zsb').find('img').remove().end().append('<img id="zsb_img" style="'+css_zsb_img+'" />');
				zsb_img_url=$('#zsb-'+current).attr('href');
				zsh_img('#zsb_img',zsb_img_url,pagesize,current,zcounter);
				return false;
			});
			$('#zsb_bg,#zsb_img').click(function(){
				$('#zsb_bg,#zsb_img').unbind('click');
				$('#zsb_bg,#zsb').fadeOut(400,function(){$(this).remove();});
				return false;
			});
		}
		return false;
	});
}
function zsh_img(img_id,zsb_img_url,pagesize,current,zcounter) {
	$('#zsb_prev,#zsb_next').hide();
	$(img_id).attr('src',zsb_img_url).load(function(){
		var x = pagesize[0] - 100, y = pagesize[1] - 100;
		var img_w=$(this).width(), img_h=$(this).height();
		if (img_w > x) {
			img_h = img_h * (x / img_w);
			img_w = x;
			if (img_h > y) {
				img_w = img_w * (y / img_h);
				img_h = y;
			}
		} else if (img_h > y) {
			img_w = img_w * (y / img_h);
			img_h = y;
			if (img_w > x) {
				img_h = img_h * (x / img_w);
				img_w = x;
			}
		}
		var marginleft=-(img_w/2+5)+'px', margintop=-(img_h/2+5)+'px';
		img_w=img_w+'px', img_h=img_h+'px';
		$(this).css({"width":img_w,"height":img_h}).fadeIn(600).parent().css({"margin-left":marginleft,"margin-top":margintop}).prev().css("background-image","none");
		if (current>0) $('#zsb_prev').show();
		if (current<zcounter-1) $('#zsb_next').show();
	});
}
function zsb_getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	arrayPageSize = [w,h];
	return arrayPageSize;
}
var loadingimg = (function loadingimg(){ //获取loading图片url
	var i=0,got=-1,url,len=document.getElementsByTagName('script').length;
	while(i<=len && got==-1){
		url=document.getElementsByTagName('script')[i].src;
		got=url.indexOf('/desire.js');
		i++;
	}
	return url.replace('/desire.js','/zshowbox-loading.gif');
})();


