// set the ie or firefox flag
var ieFlag = (navigator.appVersion.indexOf("MSIE")!=-1);
var ffFlag = ((navigator.userAgent.indexOf("Gecko")!=-1) || (navigator.userAgent.indexOf("Chrome")!=-1));

// Try to resize the image
function LoadDemo(img)
{
  if (!ieFlag && !ffFlag)
  {
    img.onmouseover = '';
    img.onmouseout = '';
  }
}

// getoffset
function getObjectPos(e)
{
	var t = e.offsetTop;
	var l = e.offsetLeft;
	var w = e.offsetWidth;
	var h = e.offsetHeight;
	while (e = e.offsetParent)
	{
		t += e.offsetTop;
		l += e.offsetLeft;
	}
	return { t:t, l:l, w:w, h:h }
}

// Zoom out the demo Image if it was zoom in
function OutDemo()
{
  var demo = document.getElementById('DemoImg2');
  if (!demo) return;
  demo.style.display = 'none';
  demo.childNodes[0].onload = '';
}

function OverDemo(img, ev)
{
  var demo = document.getElementById('DemoImg2');
  if (!demo) return; 

  var jmg = demo.childNodes[0];
  demo.style.display = 'none';
  jmg.onload = function() 
  {
	var po = getObjectPos(img);
    var ex = po.l;
    var ey = po.t + po.h;
	if (img.tagName == 'IMG' && img.height == this.height && img.width == this.width)
	{
		img.onmouseover = '';
		img.onmouseout = '';
		return;
	}
    if (typeof img.alt == 'undefined' || img.alt.indexOf('RECOMMEND') == -1)
    {
      if ((ex - document.body.scrollLeft + po.w + this.width) > document.body.clientWidth)
        ex = ex - this.width - 2;
      else
        ex = ex + po.w + 2;

      if ((ey - document.body.scrollTop - this.height) >= 0)
		ey = ey - this.height;
	  else
		ey = ey - po.h;

	  if (ex < 0) ex = po.l + po.w + 2;
	  if (ey < 0) ey = po.t;
    }
    demo.style.left = String(ex) + 'px';
    demo.style.top = String(ey) + 'px';
    demo.style.display = '';
  };
  
  jmg.src = (img.tagName == 'IMG' ? img.src.replace('_s.', '.') : img.href); 

  /*
  var ex = (ieFlag ? (ev.clientX + document.body.scrollLeft) : ev.pageX);
  var ey = (ieFlag ? (ev.clientY + document.body.scrollTop) : ev.pageY);
  if (img.alt.indexOf('RECOMMEND') != -1) ey += 40; 
  else ey -= (jmg.height + 30);
  if ((ex + jmg.width) > document.body.clientWidth) ex = ex - jmg.width - 100;
  if (ex < 0) ex = 0; 

  demo.style.left = String(ex) + 'px';
  demo.style.top = String(ey) + 'px';  
  demo.style.display = '';
  */
}

// Save the image to Local disk
function SaveImage(img)
{
	var win = document.getElementById('genFrm').contentWindow;
	if (!win || img.tagName != 'IMG') return;
	win.location.href = img.src;
	setTimeout(function() { win.document.execCommand("SaveAs"); }, 200);
}

// 复制图片到剪贴版
function CopyImage(img)
{
  if (img.tagName != 'IMG') return;
  if (typeof img.contentEditable == 'undefined' || !document.body.createControlRange)
  {
    alert('抱歉，浏览器不支持直接复制图片！\n请将鼠标移到图片上方，单击鼠标右键在弹出菜单中选择“复制”');
  }
  else
  {
    var ctrl = document.body.createControlRange();
    img.contentEditable = true;
    ctrl.addElement(img);
    ctrl.execCommand('Copy');
    img.contentEditable = false;
    alert('复制完成，到QQ对话框里按Ctrl-V就可以啦！\n\n若不能粘贴请重复尝试或用鼠标右键选复制');
  }
}

// 发送到手机
function MmsImage(img)
{
  // mmslink
  var url = img.src;
  var pos = url.indexOf('?');
  if (pos > 0) url = url.substring(0, pos);
  //window.open('http://tlt.7town.com/ZComImgDIY/step2.asp?imageUrl=' + encodeURIComponent(url) + '&uid=20398&a=&b=&c=&d=&e=&f=', '_blank');
  window.open('http://www.zhangxiu.com/mms/diy.html?IsLogo=&imgurl=' + encodeURIComponent(url) + '&f=13746','_blank');
}

function GetFavoriteImageSrc()
{
  var p = '';
  if (document.getElementById('backImage2')) 
    p = document.getElementById('backImage2').src;
  else if (document.getElementById('backImage')) 
    p = document.getElementById('backImage').src;
  else if(document.getElementById('DemoImg'))
    p = document.getElementById('DemoImg').src;
  return p;
}

function FavoriteShare(id, ttl, loc, pic)
{
  //parameters
  var u = loc || document.location; 
  var t = ttl || document.title;
  var p = pic || '';
  var w = 440;
  var h = 430;
  var f = 'http://';

  // where to share?
  t = t.replace('(czxiu.com)', '');
  if (id == 'qzone' || id == 'tsina' || id == 'tqq')
    t = t.replace('彩字秀', '#彩字秀#');
  var e = encodeURIComponent;
  if (id == 'tsina')
  {
    if (p == '') p = GetFavoriteImageSrc();
    f += 'v.t.sina.com.cn/share/share.php?appkey=80693819';
    f += '&url=' + e(u) + '&title=' + e(t);
    f += '&source=&sourceUrl=&content=utf-8&pic=' + e(p);
  }
  else if (id == 'tqq')
  {
    if (p == '') p = GetFavoriteImageSrc();
    f += 'v.t.qq.com/share/share.php?title='+ e(t) + '&url=' + e(u);
    f += '&appkey=729f40c568d74376a7bebe561e732fb3&site=www.czxiu.com&pic=' + e(p);   
    w = 700;
    h = 580;
  }
  else if (id == 'douban')
  {
    f += 'www.douban.com/recommend/?';
    f += 'url=' + e(u) + '&title=' + e(t);
  }
  else if (id == 'qzone')
  {
    if (p == '') p = GetFavoriteImageSrc();
    f += 'sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
    f += 'url=' + e(u) + '&pics=' + e(p);
    w = 600;
    h = 430;
  }
  else if (id == 'qq-shuqian')
  {
    f += 'shuqian.qq.com/post?from=3';
    f += '&title=' + e(t) + '&uri=' + e(u) + 'jumpback=2&noui=1';
    w = 930;
    h = 470;
  }
  else if (id == 'itieba')
  {
    if (p == '') p = GetFavoriteImageSrc();
    f += 'tieba.baidu.com/i/sys/share?';
    f += 'link=' + e(u) + '&type=' + (p == '' ? 'text' : 'picture') + '&title=' + e(t);
    if (p != '') f += '&content=' + e(p);
    w = 400;
    h = 500;
  }
  else if (id == 'hi-baidu')
  {
    f += 'apps.hi.baidu.com/share/?';
    f += 'url=' + e(u) + '&title=' + e(t);
    w = 360;
  }
  else if (id == 'kaixin001')
  {
    f += 'www.kaixin001.com/repaste/share.php?';
    f += 'rtitle=' + e(t) + '&rurl=' + e(u) + '&rcontent=' + e(p == '' ? t : p);
    w = 520;
  }
  else if (id == 'renren')
  {
    f += 'share.renren.com/share/buttonshare.do?';
    f += 'link=' + e(u) + '&title=' + e(t);
    w = 520;
  }
  else if (id == 'cang-baidu')
  {
    f += 'cang.baidu.com/do/add?';
    f += 'it=' + e(t) + '&iu=' + e(u) + '&dc=' + e(p == '' ? t : p) + '&tn=%E5%BD%A9%E5%AD%97%E7%A7%80' + '&fr=bk#nw=1';
    w  = 540;
  }
  else if (id == 'copyurl')
  {
	var msg = '好，这东西挺有意思，看看吧！\n' + t + '\n' + u;
	try {
		window.clipboardData.setData("text", msg);
		alert("复制成功，按 Ctrl-V 即可粘贴送出！"); 
	}
	catch (e) { 
		alert("抱歉，当前浏览器不支持复制功能！"); 
	}
	return;
  }
  else
  {
    // local favorite
    if (typeof document.all != 'undefined') window.external.addFavorite(u, t);
    else if (typeof window.sidebar != 'undefined') window.sidebar.addPanel(t, u, '');
    return;
  }

  //do-open or change location.href
  var x = Math.round((screen.width - w)/2);
  var y = Math.round((screen.height - h)/2);
  var s = 'toolbar=0,status=0,resizable=1,width=' + w + ',height=' + h + ',left=' + x + ',top=' + y;
  if (!window.open(f, 'wm_' + id, s)) u.href = f;
}

function ShowRRLike()
{
  var url = window.location.href;
  if ((pos = url.indexOf('?')) > 0) url = url.substring(0, pos);
  var src = 'http://www.connect.renren.com/like?url=' + encodeURIComponent(url) + '&showfaces=false';
  var fav = document.getElementById('FavShare');
  var rrl = document.getElementById('RenRenLike');
  if (fav && rrl)
  {
  	fav.style.width = (parseInt(fav.style.width)+110) + 'px';
  	rrl.src = src;
  	rrl.style.display = '';
  }
}

