﻿var Site = {
	init: function() {
		if ( $('liquid') )    Liquid.parseLiquids();	
		if ( $('folioMenu') ) Folio.init();
		if ( $('top_share')	)
		{
		    $('top_share').onclick = function () {
		        //overdiv=1;
		        showShare(findPosY(this),findPosX(this));		        
		        hideEmail();
		    }		    
		}
		if ( $('top_email')	)
		{
		    $('top_email').onclick = function () {
		        //overdiv=1;
		        showEmail(findPosY(this),findPosX(this));
		        hideShare();
		    }		    
		}
		if ( $('commentForm') )
		{
		    if (GetCookie('c_name') != null) {
		        $('name').value = GetCookie('c_name');
		        $('person').checked = true
		    }
		    if (GetCookie('c_email') != null) {
		        $('email').value = GetCookie('c_email');
		    }
		    if (GetCookie('c_url') != null) {
		        $('url').value = GetCookie('c_url');
		    }
		}
		if ( $('myGallery') )
		{			
            var myGallery = new gallery($('myGallery'), {
					timed: false,
        			showArrows: true,
        			showCarousel: true,
        			embedLinks: false,
        			showInfopane: false
				});
		}
		if ( $('imgVote') )
		{		    
		    if (GetCookie("LogID" + $("logId").value) == $("logId").value) {
		        $('imgVote').src = '/images/postvote.png';
		        $('imgVote').style.cursor = "default";
                $('voteTip').innerHTML = '感谢您的投票！&nbsp;&nbsp;'; 
                $('imgVote').title = "您已经为该篇文章投过票了，谢谢您的支持！";
                $('imgVote').onclick = function () {
		            void(0);
		        }
		    } else {
		    
		        $('imgVote').onclick = function () {
		        var _str = $('logId').value;
		         
		        var url = '/blog/Vote.aspx';
		        
		         new Ajax(url,{
                       postBody: 'logId=' + _str,
                       onComplete: function(){ 
                           var _count = this.response.text;                       
                           $('voteCount').innerHTML = "+" + _count;                           
                           $('imgVote').src = '/images/postvote.png';
                           $('imgVote').style.cursor = "default";
                           $('imgVote').title = "您已经为该篇文章投过票了，谢谢您的支持！";
                           $('voteTip').innerHTML = '感谢您的投票！&nbsp;&nbsp;';
                           SetCookie("LogID" + $("logId").value,$('logId').value,10000);                       
                           $('imgVote').onclick = function () {
		                       void(0);
		                   }
                       }                
                    }).request();
		      
		        }
		    }
		}			
	}
};

var Liquid = {

	parseLiquids: function(){
		var liquids = $$('#liquid .liquid');
		var fx = new Fx.Elements(liquids, {wait: false, duration: 800, transition: Fx.Transitions.elasticOut});
		liquids.each(function(liquid, i)
		{
			liquid.addEvent('mouseover', function(e)
			{
				e = new Event(e).stop();
				var obj = {};
				obj[i] = { 'width': [liquid.getStyle('width').toInt(), 280] };

				liquids.each(function(other, j)
				{
					if (other != liquid)
					{
						var w = other.getStyle('width').toInt();
						if (w != 125) obj[j] = {'width': [w, 125]};
					}
				});
				fx.start(obj);
			});
		});

		document.addEvent('mouseover', function(e){
			e = new Event(e);
			var rel = e.relatedTarget;
			if (!rel) return;
			if (rel.hasClass && (rel.hasClass('liquids') || rel.hasClass('liquid') || rel.id == 'liquid')){
				var obj = {};
				liquids.each(function(other, j){
					obj[j] = {'width': [other.getStyle('width').toInt(), 178]};
				});
				fx.start(obj);
			};
		});
	}
};

var Folio =
	{
		init: function()
		{
			Folio.parseItems();
		},

		parseItems: function()
		{
			var first = true;
			var items = $$('#folioMenu .folioItem');
			var fx = new Fx.Elements(items, {wait: false, duration: 300, transition: Fx.Transitions.quadInOut});
			items.each( function( fitem, i)
			{
				var myFx = new Fx.Style(fitem, 'opacity').set(1);

				fitem.addEvent('mouseover', function(e)
				{
					e = new Event(e).stop();
					var obj = {};
					if( fitem.getStyle('opacity') != 0.5 )
					{
						obj[i] = { 'opacity': [1, 0.5] };
					}

					items.each( function( other, j)
					{
						if( other != fitem )
						{
							if( other.getStyle('opacity') != 1 )
							{
								obj[j] = {'opacity': [other.getStyle('opacity'), 1]};
							}
						}
					});
					fx.start(obj);
				});

				fitem.addEvent('mouseout', function(e)
				{
					e = new Event(e).stop();
					var obj = {};

					items.each( function( other, j)
					{
							if( other.getStyle('opacity') != 1 )
							{
								obj[j] = {'opacity': [other.getStyle('opacity'), 1]};
							}
					});
					fx.start(obj);
				});
			});

		}
	};
window.onDomReady(Site.init);

Window.onDomReady(function() {
 var tips = new Tips($$('.tips'));
});

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}
//function SetCookie(name,value,expires,path,domain,secure)
function SetCookie(name,value,expires)
{
    var expDays = expires*24*60*60*1000;
    var expDate = new Date();
    expDate.setTime(expDate.getTime()+expDays);
    var expString = ((expires==null) ? "" : (";expires="+expDate.toGMTString()))
    //var pathString = ((path==null) ? "" : (";path="+path))
    //var domainString = ((domain==null) ? "" : (";domain="+domain))
    //var secureString = ((secure==true) ? ";secure" : "" )
    document.cookie = name + "=" + escape(value) + expString;// + pathString + domainString + secureString;
} 


function GetCookie(name)
{
    var result = null;
    var myCookie = document.cookie + ";";
    var searchName = name + "=";
    var startOfCookie = myCookie.indexOf(searchName);
    var endOfCookie;
    if (startOfCookie != -1){
        startOfCookie += searchName.length;
        endOfCookie = myCookie.indexOf(";",startOfCookie);
        result = unescape(myCookie.substring(startOfCookie, endOfCookie));
    }
    return result;
} 


function ClearCookie(name)
{
    var ThreeDays=3*24*60*60*1000;
    var expDate = new Date();
    expDate.setTime(expDate.getTime()-ThreeDays);
    document.cookie=name+"=;expires="+expDate.toGMTString();
} 

