//make email address avoid spamming
var tld_ = new Array()
tld_[0] = "com";
tld_[1] = "co.uk";
tld_[2] = "co.in";
tld_[3] = "co.id";
tld_[4] = ".com.au";
tld_[5] = ".com.cn";
tld_[6] = ".com.hk";
tld_[7] = ".com.mx";
tld_[8] = "de";
var topDom_ = 13;
var m_ = "mailto:";
var a_ = "@";
var d_ = ".";
function mail(name, dom, tl, params){
	var s = eform(name,dom,tl);
	document.write('<a href="'+m_+s+params+'">'+s+'</a>');
}
function mail2(name, dom, tl, params, display){
	document.write('<a href="'+m_+eform(name,dom,tl)+params+'">'+display+'</a>');
}
function eform(name, dom, tl){
	var s = name+a_;
	if (tl!=-2){
		s+= dom;
		if (tl>=0)s+= d_+tld_[tl];
	}
	else s+= swapper(dom);
	return s;
}
function swapper(d){
	var s = "";
	for (var i=0; i<d.length; i+=2)
		if (i+1==d.length)
			s+= d.charAt(i)
		else
			s+= d.charAt(i+1)+d.charAt(i);
	return s.replace(/\?/g,'.');
}

//tool box load
$(document).ready(function(){
	var righttext=$('#right_box').html();
	$('#right_loading').html(righttext);
	var lefttext=$('#left_box').html();
	$('#left_loading').html(lefttext);
	var righttext1=$('#right_box1').html();
	$('#right_loading1').html(righttext1);
	
});

//font size
$(document).ready(function(){
     var setFont = getCookie("fontsize");
     if(!setFont){
      setCookie("fontsize",10,15);
      setFont = 10;   
     }else{
         $("#bodytext1").css("font-size",setFont+"px");
         $("#bodytext2").css("font-size",setFont+"px");
         $("#bodytext3").css("font-size",setFont+"px");
     }
   //Top Menu  
   $("#tab_nav_ul li").mouseover(function(){    $(this).find("a").attr("class","current");   });
   $("#tab_nav_ul li").mouseout(function(){     $(this).find("a").attr("class","current1");  }); 
   $(".fontplus").click(function(){
	   if(setFont<30){setFont++;setFont++;}
	   setCookie("fontsize",setFont,15);
	   $("#bodytext1").css("font-size",setFont+"px");
	   $("#bodytext2").css("font-size",setFont+"px");
	   $("#bodytext3").css("font-size",setFont+"px");
	   
   });
   $(".fontminus").click(function(){
	   if(setFont>8){setFont--;setFont--;}
	   setCookie("fontsize",setFont,15);
	   $("#bodytext1").css("font-size",setFont+"px");
	   $("#bodytext2").css("font-size",setFont+"px");
	   $("#bodytext3").css("font-size",setFont+"px");
   });   
});

//stats
function article_counter(article_id,category_id,country_code,country_id){
	$.get("/art/ajax/article_counter.php", { article_id: article_id, maincat_id: category_id, country_code: country_code, country_id: country_id } );
}


var shareartcle = function(site){
	switch(site){
        case "twitter":
            OpenPop('http://twitter.com/home?status='+exHed+'+'+exURL,'twitter','toolbar=no,resizable=yes,scrollbars=yes,width=850,height=500');
            break; 
        case "digg":
            OpenPop('http://digg.com/remote-submit?phase=2&url='+exURL+'&title='+exHed+'&bodytext='+exDek,'digg','toolbar=no,resizable=yes,scrollbars=yes,width=850,height=500');
            break;        
        case "newsvine":
            OpenPop('http://www.newsvine.com/_wine/save?ver=2&popoff=0&aff=ibtimes&t=' + keywords + '&e=' + exDek + '&h=' + exHed + '&u=' + exURL, 'newsvine', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "delicious":
            OpenPop('http://del.icio.us/post?tags=test&v=4&noui&jump=close&url='+exURL+'&title='+exHed+'&notes='+exDek+'&tags='+keywords, 'delicious','toolbar=no,resizable=yes,scrollbars=yes,width=850,height=500');
            break;        
        case "facebook":
            OpenPop('http://www.facebook.com/sharer.php?u=' + exURL + '&t=' + exHed, 'facebook', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "stumbleupon":
            OpenPop('http://www.stumbleupon.com/submit?url=' + exURL + '&title=' + exHed, 'stumbleupon', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "yahoo":
            OpenPop('http://buzz.yahoo.com/submit?submitUrl=' + exURL + '&submitHeadline=' + exHed+ '&submitSummary=' + exDek, 'yahoo', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "myspace":
            OpenPop('http://www.myspace.com/Modules/PostTo/Pages/?u=' + exURL + '&t=' + exHed+ '&c=' + exDek, 'myspace', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "google":
            OpenPop('http://www.google.com/bookmarks/mark?op=add&bkmk=' + exURL + '&title=' + exHed+ '&annotation=' + exDek, 'google', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "reddit":
            OpenPop('http://www.reddit.com/submit?url=' + exURL + '&title=' + exHed, 'reddit', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;        
        case "linkedin":
            OpenPop('http://www.linkedin.com/shareArticle?mini=true&url=' + exURL + '&title=' + exHed+ '&summary=' + exDek, 'linkedin', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "technorati":
            OpenPop('http://technorati.com/faves/inistone?add=' + exURL, 'technorati', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "yahoobookmk":
            OpenPop('http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&u=' + exURL+'&t='+ exHed, 'yahoobookmk', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "mixx":
            OpenPop('http://www.mixx.com/submit?page_url=' + exURL+'&t='+ exHed, 'mixx', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "yahoomyweb":
            OpenPop('http://myweb2.search.yahoo.com/myresults/bookmarklet?u=' + exURL+'&t='+ exHed, 'yahoomyweb', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "propeller":
            OpenPop('http://www.propeller.com/submit/?U=' + exURL+'&T='+ exHed, 'propeller', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "propeller":
            OpenPop('http://www.propeller.com/submit/?U=' + exURL+'&T='+ exHed, 'propeller', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "windowslive":
            OpenPop('https://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=' + exURL+'&title='+ exHed+'&top=1', 'windowslive', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "friendfeed":
            OpenPop('http://friendfeed.com/share?url=' + exURL+'&title='+ exHed, 'friendfeed', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "furl":
            OpenPop('http://furl.net/storeIt.jsp?u=' + exURL+'&t='+ exHed, 'furl', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "xanga":
            OpenPop('http://www.xanga.com/private/editorx.aspx?u=' + exURL+'&t='+ exHed+'&s='+ exDek, 'xanga', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;
        case "blinklist":
            OpenPop('http://blinklist.com/index.php?Action=Blink/addblink.php&Url=' + exURL+'&Title='+ exHed, 'blinklist', 'toolbar=0,status=0,height=500,width=850,scrollbars=yes,resizable=yes');
            break;	}
}

function OpenPop(url, name, params) {
	var win = window.open(url, name, params);
}


//image resize
function findPos(obj) {
	var pos = new Array();
	pos[0] = pos[1] = 0;
	if (obj.offsetParent) {
		pos[0] = obj.offsetLeft
		pos[1] = obj.offsetTop
		while (obj = obj.offsetParent) {
			pos[0] += obj.offsetLeft
			pos[1] += obj.offsetTop
		}
	}	
	return pos;
}
//photo resize(like graph somthing)
var container_img;
function photos_realsize(Obj,val) { 
	if(!container_img){
		container_img = document.createElement("div");
		document.body.appendChild(container_img);
		container_img.className="artimg";
		container_img.style.position = "absolute";
		container_img.onclick = pimg_close;
	}else{ 
		container_img.innerHTML = '';
		container_img.style.display = "block";
	}
	container_img.style.left = (findPos(Obj)[0]-50)+"px";
	container_img.style.top  = (findPos(Obj)[1])+"px";
	container_img.innerHTML = "<img src='"+val+"' class='hand'>";
} 
function pimg_close(){
	container_img.style.display = "none";
}
 //new photo resize
 var img_orgsize;
 var img_newsize;
 function resizeImg(Obj,size) {
 img_orgsize=Obj.width;
 Obj.name = Obj.width;
 Obj.width=size;
 img_newsize=size;
 Obj.onclick = function(){
  if(!container_img){
   container_img = document.createElement("div");
   document.body.appendChild(container_img);
   container_img.className="artimg";
   container_img.style.position = "absolute";
   container_img.onclick = pimg_close;
  }else{ 
   container_img.innerHTML = '';
   container_img.style.display = "block";
  }
  container_img.style.left = (findPos(Obj)[0]+(img_newsize/2)-(Obj.name/2))+"px";
  container_img.style.top  = (findPos(Obj)[1])+"px";
  container_img.innerHTML = "<img src='"+Obj.src+"' class='hand'>"; 
 }
 }

 //rates
$(document).ready(function(){
    $(".rates_btn").click(function(){
		$(this).blur();
        var id=$(this).attr("id");
		var text = getCookie('ratethis');
		if(in_text(text,article_id)=="F"){
			setCookie('ratethis',text+','+article_id,'0.1');
			$('#rate_'+id).load('http://www.ibtimes.com/art/ajax/article_rate.php?article_id='+article_id+'&direction='+id, null, function(responseText){
				var text_new=$('#rate_'+id).html();
				$('#rate_'+id+'2').html(text_new);
				$('#rate_'+id+'3').html(text_new);
			});
		}else{
			alert("You already voted to this article.");
		}
    });
});
function in_text(text,needle){
	var result="F";
	if(text==null)result="F";
	else if(text){
		var id_arr  = text.split(',');
		for(var i=0;i<id_arr.length;i++){
			if(needle==id_arr[i]&&id_arr[i])result="T";
		}
	}
	return result;
}

//image enlarge
$(document).ready(function(){
    var totalimg    = $(".imglist").children().length;
    var curimg      = 1;
    var pastimg     =  0;

    $("#boxtoolbar .close").click(function(){
		   $(".imglist #big_photo_"+curimg).hide("slow"); 
           $("#photo_big").toggle("slow");  
		  // $(".imglist #big_photo_"+curimg).css("display","none");
           setTimeout('$("#left_loading").show()',500);
    });
    $("#enlarge1").click(function(){
        $("#left_loading").hide();
        var str = "";
        for(var i=1;i <= totalimg;i++){
            str +="<li id=\"li_photo_"+i+"\"><a href=\"javascript:;\">"+i+"</a></li>";
        }
        $(".toolnav ul").html(str);
        $(".toolnav ul #li_photo_1").addClass("current");
        $(".toolnav ul li").click(function(){
            var idstring = $(this).attr('id');
            var idsplit  = idstring.split('_');
            var idnum    = idsplit[2];
            if(curimg != idnum){
                pastimg = curimg;
                curimg =  idnum;
				var margintop=eval($(".imglist #big_photo_"+curimg+" > img").attr('id'))+17;
				//$("#photo_big").css("height",margintop+37);
				//$("#boxtoolbar").css("margin-top",margintop);
				$(".toolnav ul #li_photo_"+curimg).addClass("current");
				$(".toolnav ul #li_photo_"+pastimg).removeClass("current");
                $(".imglist #big_photo_"+curimg).fadeIn("slow"); 
                $(".imglist #big_photo_"+pastimg).fadeOut("slow");                
            }
        });
		var margintop=eval($(".imglist #big_photo_"+curimg+" > img").attr('id'))+17;
		//$("#photo_big").css("height",margintop+37);
		$("#boxtoolbar").css("margin-top",margintop);
        $(".imglist #big_photo_"+curimg).hide(); 
        $("#photo_big").toggle("slow");
        $(".imglist #big_photo_"+curimg).show("slow"); 
    });
});

//Video enlarge
$(document).ready(function(){
    $("#video_big .close").click(function(){
           $("#video_big").toggle("slow");  
           setTimeout('$("#left_loading").show()',500);
    });
    $("#enlarge2").click(function(){
		alert(videothumb);
        $("#left_loading").hide();
        $("#video_big").toggle("slow");
    });
});

/* jQuery Image Magnify script v1.1 - Article Image Enlarge
* Author: Dynamic Drive at http://www.dynamicdrive.com/
* Visit http://www.dynamicdrive.com/ for full source code
*/
jQuery.imageMagnify={
	dsettings: {
		magnifyby: 1.5, //default increase factor of enlarged image
		duration: 500, //default duration of animation, in millisec
		imgopacity: 0.2 //opacify of original image when enlarged image overlays it
 	},
	cursorcss: '-moz-zoom-in', //Value for CSS's 'cursor' attribute, added to original image
	zIndexcounter: 100,
	refreshoffsets:function($window, $target, warpshell){
		var $offsets=$target.offset()
		var winattrs={x:$window.scrollLeft(), y:$window.scrollTop(), w:$window.width(), h:$window.height()}
		warpshell.attrs.x=$offsets.left //update x position of original image relative to page
		warpshell.attrs.y=$offsets.top
		warpshell.newattrs.x=winattrs.x+winattrs.w/2-warpshell.newattrs.w/2
		warpshell.newattrs.y=winattrs.y+winattrs.h/2-warpshell.newattrs.h/2
		if (warpshell.newattrs.x<winattrs.x+5){ //no space to the left?
			warpshell.newattrs.x=winattrs.x+5	
		}
		else if (warpshell.newattrs.x+warpshell.newattrs.w > winattrs.x+winattrs.w){//no space to the right?
			warpshell.newattrs.x=winattrs.x+5
		}
		if (warpshell.newattrs.y<winattrs.y+5){ //no space at the top?
			warpshell.newattrs.y=winattrs.y+5
		}
	},
	magnify:function($, $target, options){
		var setting={} //create blank object to store combined settings
		var setting=jQuery.extend(setting, this.dsettings, options)
		var attrs=(options.thumbdimensions)? {w:options.thumbdimensions[0], h:options.thumbdimensions[1]} : {w:$target.outerWidth(), h:$target.outerHeight()}
		var newattrs={}
		newattrs.w=(setting.magnifyto)? setting.magnifyto : Math.round(attrs.w*setting.magnifyby)
		newattrs.h=(setting.magnifyto)? Math.round(attrs.h*newattrs.w/attrs.w) : Math.round(attrs.h*setting.magnifyby)
		$target.css('cursor', jQuery.imageMagnify.cursorcss)
		if ($target.data('imgshell')){
			$target.data('imgshell').$clone.remove()
			$target.css({opacity:1}).unbind('click.magnify')
		}	
		var $clone=$target.clone().css({position:'absolute', left:0, top:0, visibility:'hidden', border:'1px solid gray', cursor:'pointer'}).appendTo(document.body)
		$clone.data('$relatedtarget', $target) //save $target image this enlarged image is associated with
		$target.data('imgshell', {$clone:$clone, attrs:attrs, newattrs:newattrs})
		$target.bind('click.magnify', function(e){ //action when original image is clicked on
			var $this=$(this).css({opacity:setting.imgopacity})
			var imageinfo=$this.data('imgshell')
			jQuery.imageMagnify.refreshoffsets($(window), $this, imageinfo) //refresh offset positions of original and warped images
			var $clone=imageinfo.$clone
			$clone.stop().css({zIndex:++jQuery.imageMagnify.zIndexcounter, left:imageinfo.attrs.x, top:imageinfo.attrs.y, width:imageinfo.attrs.w, height:imageinfo.attrs.h, opacity:0, visibility:'visible'})
			.animate({opacity:1, left:imageinfo.newattrs.x, top:imageinfo.newattrs.y, width:imageinfo.newattrs.w, height:imageinfo.newattrs.h}, setting.duration,
			function(){ //callback function after warping is complete
				//none added		
			}) //end animate
		}) //end click
		$clone.click(function(e){ //action when magnified image is clicked on
			var $this=$(this)
			var imageinfo=$this.data('$relatedtarget').data('imgshell')
			jQuery.imageMagnify.refreshoffsets($(window), $this.data('$relatedtarget'), imageinfo) //refresh offset positions of original and warped images
			$this.stop().animate({opacity:0, left:imageinfo.attrs.x, top:imageinfo.attrs.y, width:imageinfo.attrs.w, height:imageinfo.attrs.h},  setting.duration,
			function(){
				$this.hide()
				$this.data('$relatedtarget').css({opacity:1}) //reveal original image
			}) //end animate
		}) //end click
	}
};
jQuery.fn.imageMagnify=function(options){
	var $=jQuery
	return this.each(function(){ //return jQuery obj
		var $imgref=$(this)
		if (this.tagName!="IMG")
			return true //skip to next matched element
		if (parseInt($imgref.css('width'))>0 && parseInt($imgref.css('height'))>0 || options.thumbdimensions){ //if image has explicit width/height attrs defined
			jQuery.imageMagnify.magnify($, $imgref, options)
		}
		else if (this.complete){ //account for IE not firing image.onload
			jQuery.imageMagnify.magnify($, $imgref, options)
		}
		else{
			$(this).bind('load', function(){
				jQuery.imageMagnify.magnify($, $imgref, options)
			})
		}
	})
};
jQuery.fn.applyMagnifier=function(options){ //dynamic version of imageMagnify() to apply magnify effect to an image dynamically
	var $=jQuery
	return this.each(function(){ //return jQuery obj
		var $imgref=$(this)
		if (this.tagName!="IMG")
			return true //skip to next matched element
		
	})	

};
//** The following applies the magnify effect to images with class="magnify" and optional "data-magnifyby" and "data-magnifyduration" attrs
//** It also looks for links with attr rel="magnify[targetimageid]" and makes them togglers for that image
jQuery(document).ready(function($){
	var $targets=$('.magnify')
	$targets.each(function(i){
		var $target=$(this)
		var options={}
		if ($target.attr('data-magnifyto'))
			options.magnifyto=parseFloat($target.attr('data-magnifyto'))
		if ($target.attr('data-magnifyby'))
			options.magnifyby=parseFloat($target.attr('data-magnifyby'))
		if ($target.attr('data-magnifyduration'))
			options.duration=parseInt($target.attr('data-magnifyduration'))
		$target.imageMagnify(options)
	})
	var $triggers=$('a[rel^="magnify["]')
	$triggers.each(function(i){
		var $trigger=$(this)
		var targetid=$trigger.attr('rel').match(/\[.+\]/)[0].replace(/[\[\]']/g, '') //parse 'id' from rel='magnify[id]'
		$trigger.data('magnifyimageid', targetid)
		$trigger.click(function(e){
			$('#'+$(this).data('magnifyimageid')).trigger('click.magnify')
			e.preventDefault()
		})
	})
})
