$("document").ready(function () {

    $('.tps-n').tipsy({ fade: true, gravity: 'n' });
    $('.tps-s').tipsy({ fade: true, gravity: 's' });

    //headline
    function headline(obj) {
        var thisobj = this;
        this.navItems = $('#' + obj + ' .nav .navItems a');
        this.headlineLength = this.navItems.length;
        this.objWidth = $('#' + obj + ' .headlineItems div img').outerWidth();
        this.wfu = new waitForUser(this);
        $('#' + obj + ' .headlineItems div:eq(0)').css('left', '0');
        $('#' + obj + ' .nav .navItems a:eq(0)').addClass('selected');
        this.currentIndex = 0;



        this.navItems.each(function (index, value) {
            $(value).click(function () {
                thisobj.toggle(index);
                thisobj.wfu.init();
            })
        });

        this.toggle = function (ID) {

            if (ID != this.currentIndex) {
                var s;
                var e;
                if (ID < thisobj.currentIndex) {
                    s = -1 * thisobj.objWidth;
                    e = thisobj.objWidth;
                } else {
                    s = thisobj.objWidth;
                    e = -1 * thisobj.objWidth;
                }


                var oldObj = $('#' + obj + ' .headlineItems div:eq(' + this.currentIndex + ')');
                var newObj = $('#' + obj + ' .headlineItems div:eq(' + ID + ')');
                oldObj.animate({ left: e + 'px' }, 200);
                newObj.css('left', s + 'px');
                newObj.animate({ left: '0px' }, 400);

                var oldNavObj = $('#' + obj + ' .nav .navItems a:eq(' + this.currentIndex + ')');
                var newNavObj = $('#' + obj + ' .nav .navItems a:eq(' + ID + ')');
                oldNavObj.removeClass('selected');
                newNavObj.addClass('selected');


                thisobj.currentIndex = ID;
            }

        }

        this.swapNext = function () {
            if (thisobj.currentIndex >= thisobj.headlineLength - 1) {
                thisobj.toggle(0);
            } else {
                thisobj.toggle(thisobj.currentIndex + 1);
            }

        }

    }
    //headline close
    var hdln = new headline('headline');
    var mhdln = new headline('miniHeadline');
    //////////////////////////////////////////




    //-----------------waitForUser---------------------//
    function waitForUser(obj) {
        this.played = true;
        this.resume = null;

        this.init = function () {
            this.played = false;
            clearInterval(this.resume);
            this.resume = setInterval(this.playAgain, 16000);
        }

        this.playAgain = function () {
            obj.wfu.played = true;
            clearInterval(obj.wfu.resume);
        }

    }
    ////////////////////////////
    //waitForUser close


    //-----------------site  timer---------------------//
    function siteTimer() {
        this.swap = function () {

            if (hdln.wfu.played) {
                hdln.swapNext();
            }
            if (mhdln.wfu.played) {
                mhdln.swapNext();
            }
        }
        this.slider = setInterval(this.swap, 4000);
    }
    ////////////////////////////
    var timer = new siteTimer();
    //site timer close





	
	
	
	
	//-----------------comments---------------------//
    function newsComments(obj) {
        var thisObj = this;
        this.expandObj = $('.news-comments .newComment .expand');
        this.textAreaObj = $('.news-comments .'+obj+' .textArea textarea');
        this.cancelNewCommentObj = $('.news-comments .newComment .expand .cancel');
		this.cancelReplyCommentObj = $('.news-comments .replyComment .expand .cancel');
        this.toolsObjs = $('.news-comments .commentItems li .tools');
        this.commentItemObj = $('.news-comments .commentItems li');
        this.charstatusObj = $('.news-comments .'+obj+' .charStatus span');
		

        this.defaultText = this.textAreaObj.html();

        this.toolsObjs.css('opacity', '0.35');
		if(obj == 'newComment'){
        	this.expandObj.css("display", "none");
		}
		if(obj == 'replyComment')
		{
			$('.'+obj).css('display','none');
		}
		


        $('.news-comments .newComment .textArea textarea').click(function () {
           	thisObj.expandObj.slideDown('fast');
			if(thisObj.textAreaObj.val() == thisObj.defaultText){
				thisObj.textAreaObj.val('');
			}
            
            thisObj.updateStatus();
        });

        this.cancelNewCommentObj.click(function () {
            thisObj.expandObj.slideUp('fast');
            thisObj.textAreaObj.val(thisObj.defaultText);
        });
		this.cancelReplyCommentObj.click(function () {
            $('#replyObj').slideUp('fast');
        });

        this.commentItemObj.mouseover(function () {
            $(this).find('.tools:first').stop().fadeTo('fast', 1);
            return false;
        });
        this.commentItemObj.mouseout(function () {

            $(this).find('.tools:first').stop().fadeTo('fast', 0.35);
            return false;
        });

        this.textAreaObj.keyup(function () {
            thisObj.updateStatus();
        });
        this.updateStatus = function () {
            var remainChar = 500 - thisObj.textAreaObj.val().length;
            if (remainChar <= 0) {
                thisObj.charstatusObj.addClass('red');
            } else {
                thisObj.charstatusObj.removeClass('red');
            }
            thisObj.charstatusObj.html(remainChar);
        }
		
		
		this.initReplyButton = function()
		{
			$('.news-comments .commentItems li .reply a').each(function (index, value) {
				$(value).click(function () {
					var commentObj = $(this).parent().parent().parent();
					$('#replyObj').insertAfter($(commentObj));
					$('#replyObj').css('display','none');
					$('#replyObj').slideDown('fast');
				})
			});
		}





    }
	///////////////////////////
    var newComment = new newsComments('newComment');
	var replyComment = new newsComments('replyComment');
	//site timer close
	replyComment.initReplyButton();








	//-----------------comments---------------------//
	function searchInput(obj)
	{
		$(obj).addClass('noFocus');
		this.defaultValue = $(obj).val();
		$(obj).focus(function(){
				if($(obj).val() == this.defaultValue){
				$(obj).val('');
				$(obj).removeClass('noFocus');
			}
		})
		$(obj).blur(function(){
				if($(obj).val() == ''){
				$(obj).val(this.defaultValue);
				$(obj).addClass('noFocus');
			}
		})
		
	}
	/////////////////////////////
	var srchInpt = new searchInput('.siteTop .search .text');
	//searchInput close













	//---------------------------newsContentTools--------------------------
	function newsContentTools(){
		var thisObj = this
		this.defaultSize = 100;
		this.size = this.defaultSize;
		this.textArea = $('.newsContent .text p, .newsContent .text h1, .newsContent .text h2, .newsContent .text h3, .newsContent .text h4, .newsContent .text h5, .newsContent .text h6, .newsContent .text li');
		$('.textTools .up').click(function(){
			nct.size = nct.size+20;
			if(nct.size > 160)
			{
				nct.size = 160;
			}
			$(thisObj.textArea).css('font-size',nct.size+'%')
		});
		$('.textTools .down').click(function(){
			nct.size = nct.size-20;
			if(nct.size < 60)
			{
				nct.size = 60;
			}
			$(thisObj.textArea).css('font-size',nct.size+'%')
		});
		$('.textTools .normal').click(function(){
			nct.size = 100;
			$(thisObj.textArea).css('font-size',nct.size+'%')
		});
		$('.textTools .print').click(function(){
			window.print();
		});
		
	}
	////////////
	var nct = new newsContentTools();
	//newsContentTools




});



















//tipsy------------------------------------------
(function($) {
    $.fn.tipsy = function(options) {

        options = $.extend({}, $.fn.tipsy.defaults, options);
        
        return this.each(function() {
            
            var opts = $.fn.tipsy.elementOptions(this, options);
            
            $(this).hover(function() {

                $.data(this, 'cancel.tipsy', true);

                var tip = $.data(this, 'active.tipsy');
                if (!tip) {
                    tip = $('<div class="tipsy"><div class="tipsy-inner"/></div>');
                    tip.css({position: 'absolute', zIndex: 100000});
                    $.data(this, 'active.tipsy', tip);
                }

                if ($(this).attr('title') || typeof($(this).attr('original-title')) != 'string') {
                    $(this).attr('original-title', $(this).attr('title') || '').removeAttr('title');
                }

                var title;
                if (typeof opts.title == 'string') {
                    title = $(this).attr(opts.title == 'title' ? 'original-title' : opts.title);
                } else if (typeof opts.title == 'function') {
                    title = opts.title.call(this);
                }

                tip.find('.tipsy-inner')[opts.html ? 'html' : 'text'](title || opts.fallback);

                var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
                tip.get(0).className = 'tipsy'; // reset classname in case of dynamic gravity
                tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
                var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
                var gravity = (typeof opts.gravity == 'function') ? opts.gravity.call(this) : opts.gravity;

                switch (gravity.charAt(0)) {
                    case 'n':
                        tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                        break;
                    case 's':
                        tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                        break;
                    case 'e':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}).addClass('tipsy-east');
                        break;
                    case 'w':
                        tip.css({top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}).addClass('tipsy-west');
                        break;
                }

                if (opts.fade) {
                    tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 0.8},150);
                } else {
                    tip.css({visibility: 'visible'});
                }

            }, function() {
                $.data(this, 'cancel.tipsy', false);
                var self = this;
                setTimeout(function() {
                    if ($.data(this, 'cancel.tipsy')) return;
                    var tip = $.data(self, 'active.tipsy');
                    if (opts.fade) {
                        tip.stop().fadeOut(150, function() { $(this).remove(); });
                    } else {
                        tip.remove();
                    }
                }, 20);

            });
            
        });
        
    };
    

    $.fn.tipsy.elementOptions = function(ele, options) {
        return $.metadata ? $.extend({}, options, $(ele).metadata()) : options;
    };
    
    $.fn.tipsy.defaults = {
        fade: false,
        fallback: '',
        gravity: 'n',
        html: false,
        title: 'title'
    };
    
    $.fn.tipsy.autoNS = function() {
        return $(this).offset().top > ($(document).scrollTop() + $(window).height() / 2) ? 's' : 'n';
    };
    
    $.fn.tipsy.autoWE = function() {
        return $(this).offset().left > ($(document).scrollLeft() + $(window).width() / 2) ? 'e' : 'w';
    };
    
})(jQuery);
/////////////////////////////////////////////////////////////tipsy
