/*lazyLoadJS*/
(function(e){function g(){h=true;var b=d[0].concat(d[1],d[2]);for(i in b){var a=document.createElement("script");a.type="text/javascript";a.async=true;a.src=b[i][0];if(b[i][1])a.onload=b[i][1];var c=document.getElementsByTagName("script")[0];c.parentNode.insertBefore(a,c)}d=[[],[],[]]}var d=[[],[],[]],h=false,f=null;e.lazyLoadJS=function(b,a,c){d[c]||(c=1);a||(a=null);if(b instanceof Array)for(i in b)e.lazyLoadJS(b[i],a,c);else{d[c].push([b,a]);if(h){f&&clearTimeout(f);f=setTimeout(g,200)}}return this}; e(document).ready(g)})(jQuery);

var arScrollHeadlines = false;
var addthis_share = { email_template: "email_template" };
var addthis_config = { ui_click: true };
$(document).ready(function(){
	$('.section-v').append('<span class="icnPlayVideo"/>');
	/* Ajax Tab Loading */
	var el, a, req,creq, type, tlimit;
	if ($('#tabs').length){
		type=$('#tabs li.ui-tabs-selected a').attr('href').substring(1);
		tlimit=$('div.timeLine a.selected').attr('href').substring(1);;
		$('#tabs a').click(function(){type = $(this).attr('href').substring(1);return requestData();});
		$('div.timeLine a').click(function(){tlimit = $(this).attr('href').substring(1);return requestData();});
		// Request data from server
		function requestData(){
			if(req)req=null;
			if($('#' + type + tlimit).length) updateList(false);
			else req = $.getJSON('/news/widgets.php?widget=NewsComments&type='+type+'&tlimit='+tlimit, updateList);
			return false;
		}
		// Update tab content
		function updateList(data){
			if(data){
				if(data.error)return true;
				var str='';
				for(var i=0;i < data.articles.length;i++)str+='<li><a href="/news/articles/a'+data.articles[i][0]+'.html">'+data.articles[i][1]+'<'+'/a><'+'/li>';
				$('#tabBody').find('ul').hide().end().append('<ul id="'+type+tlimit+'" class="related">'+str+'<'+'/ul>');
			}
			$('#tabBody ul').hide().filter('#' + type + tlimit).show();
			$('#tabs li').removeClass('ui-tabs-selected').filter('#'+type+'Tab').addClass('ui-tabs-selected');
			$('div.timeLine a').removeClass('selected').filter('a[href=#'+tlimit+']').addClass('selected');
		}
	}
	/* Insert Zodiac Signs */
	var zsigns = ["Aries","Taurus","Gemini","Cancer","Leo","Virgo","Libra","Scorpio","Sagittarius","Capricorn","Aquarius","Pisces"];
	str = '<form id="frmHoroscope" method="get" action="/astrology/horoscope/"><select id="zodiacSign"><option value="0">Choose your sign<'+'/option>';
	for(var i=0; i<12;i++)str+='<option value="'+zsigns[i]+'">'+zsigns[i]+'<'+'/option>';
	$('#horoscope').html(str+'<'+'/select></form>').find('#zodiacSign').change(function(){if(this.value)window.open('/astrology/horoscope/?sign='+this.value);});
	/* Share Menu */
	var sMenu = $('#shareMenu');
	if((a=$('#wrapper').offset().left) > 40)$('#wrapper').after('<div id="toTop" style="right:'+(a/2-17)+'px"></div>');
	var toTop=$('#toTop');
    var mtop = 210;var fixed=false;
    if (!($.browser.msie && $.browser.version < 7)) {
        $(window).scroll(function(e){
            if ($(window).scrollTop() >= mtop){
            	if(!fixed){fixed = true;sMenu.addClass('fixed');toTop.fadeIn();}
            }else{
            	if(fixed){fixed = false;sMenu.removeClass('fixed');toTop.fadeOut();}
            }
        });
    }else{
    	toTop.css("position", "absolute");
    	var sHeight = $(window).height() - 44;
    	$(window).scroll(function() {
            if ($(window).scrollTop() >= mtop){
            	if(!fixed){fixed = true;toTop.fadeIn();}
            	else toTop.css('top', ($(window).scrollTop() + sHeight) + "px");
            }else if(fixed){fixed = false;toTop.fadeOut();}
    	});
    }
	/* Scroll to top */
	$('#toTop').click(function(){$('html, body').animate({scrollTop:0}, 'slow');return false;});
	/* Photo Preview in Article */
	var ibox=$('#imgBox').wrap('<div id="imgBoxWrapper"/>').find('#imgBoxWrapper');
	$('#imgBoxWrapper img').hover(function(){
		var img = new Image();
		var i=$(this);
		var t=i.attr('alt');
		var a=i.parent().attr('href');
		var osize = i.attr('size').split('x');
	    var pc = Math.min(1, 250/osize[0], 250/osize[1]);
	    var size = [Math.round(osize[0]*pc), Math.round(osize[1]*pc)];
		$(img).bind('load', function() {
			$('#imgPreview').remove();
			$('#imgBoxWrapper').prepend("<div id='imgPreview'></div>").find('#imgPreview').append('<a href="'+a+'"><img src="'+this.src+'" title="'+t+'"/></a>').css({'width':size[0]+'px', 'height':size[1]+'px'});
			$('#imgPreview').mouseleave(function(){$('#imgPreview').remove();});
		});
		img.src = this.src.replace('90x60', '250');
	},function(){})
	/* Reload CAPTCHA */
	$('a.reloadCaptcha').click(function(){
		var img = $(this).find('img:first-child');
		var src = img.attr('src');
		if(creq)creq=null;
		var oldcode = src.substring((pos=src.indexOf('code=')+5), ((pos=src.indexOf('&', pos)) == -1) ? src.length : pos);
		creq = $.getJSON('/res/verify.php?reload=1&rand='+oldcode,null,function(json, status){img.attr('src', src.replace(oldcode, json.code));});
		return false;
	});
	$('.print').click(function(){
		var url = document.location.href;
		printWin = window.open('/news/print.php?id='+url.substring(40, url.indexOf('.html')), "_blank","height=200,left=100,top=100,width=200,toolbar=no,titlebar=0,status=0,menubar=no,location=no,scrollbars=1");
		return false;
	});
	/* Font Size */
	var sharecode = '<div class="addthis_toolbox addthis_default_style">';
	for(var i=1; i<=7; i++) sharecode +='<a class="addthis_button_preferred_'+i+'"></a>';
	sharecode+= '<a class="addthis_button_email"></a><a class="addthis_button_print"></a></a></div>';
	$('#articleTools').prepend('<span class="fsize br"><b>FONT SIZE:</b> <a class="fss">A</a><a class="fsm fs">A</a><a class="fsl">A</a></span>').append(sharecode);
	$('#emailThis2').click(function(){return addthis_sendto('email')});
	
	var fsize = $('.fsize a');
	//fsize.addClass('fs');
	fsize.click(function(){
		var idx = fsize.index(this);
		fsize.removeClass('fs').eq(idx).addClass('fs');
		$('#article').css('font-size', 0.8+2*idx/10+'em');
		$.cookie('curFontSize', 0.8+2*idx/10+'em');
	});
	var cc = $('#currencyConvertor');
	if(cc.length){
		$.get('/news/widgets.php?widget=CurrencyConvertor&action=update', function(data){
			cc.find('.header:first-child').after(data);
		});
	}
	$.fn.typewriter = function(fn) {
        this.each(function() {
            var str = $(this).text(), cnt = 0;
            var t=this;
            t.innerHTML = '';
            var timer = setInterval(function() {
                if (cnt >= str.length) {
                	t.innerHTML = str;
                	clearInterval(timer);
                	if(fn)fn.call();
                }else{
                	cnt += 3;
                	t.innerHTML = (str.substring(0, cnt) + '_');	
                }
            }, 100);
        });
        return this;
	};
	if ($('#scroller').length > 0 && arScrollHeadlines){
		var i = -1, scroller = $('#scroller').append('<ul><li/></ul>').find('li:first-child');
		function showHeadline(){
			i = ++i % arScrollHeadlines.length;
			var art = arScrollHeadlines[i];
			scroller.children().remove().end()
			 .append('<a href="/news/articles/a'+art[0]+'.html">'+art[1]+'</a>').children(':first-child').typewriter(function(){setTimeout(showHeadline, 8000);});
		};
		showHeadline();
	}
})

