﻿    $(document).ready(function(){
        if($(".TabContentArea").length > 0) {
	        $(".TabContentArea").hide();
	    }
 
        if($(".TabTitle").length > 0) {
		    $(".TabTitle").click(function () {			
			    if($(this).hasClass("TabTitleOpened")){ 
				    // close already opened tab
				    $("#"+this.id.replace("_Title","_Content")).slideUp("fast",closeTab(this.id));
			    }
			    else {
				    // open tab
				    var self=$("#"+this.id.replace("_Title","_Content"));
				    $(".TabContentArea").filter(function() {return this!=self}).slideUp("fast",function(){closeTab(this.id.replace("_Content","_Title"))});
				    self.slideDown("fast",openTab(this.id));
			    }
		    })
		}

		//FAQ List PageList
		if($('.faqlist').length > 0){
		    var pagesize = 5;
		    if($('.faqlist>ul>li').length > pagesize){
		        var number =  PageNumber();
		        var html = '<a href="javascript:void(0);" onclick="ViewAllRecord()">View All</a>';
		        html += '<div class="prevbtn"><img src="/images/prev_btn.jpg" /></div>';
		        html += '<ul>';
		        var pagecount = parseInt($('.faqlist>ul>li').length / pagesize);
		        if($('.faqlist>ul>li').length % pagesize > 0){
		            pagecount++;
		        }
		        if(number > pagecount){
		            number = pagecount;
		        }
		        
		        var marginHeight = 0;
		        var showHeight = 0;
		        $('.faqlist>ul>li').each(function(i){
		            if(i < (number - 1) * pagesize){
		                marginHeight += $(this).height() + 10;
		            }
		            else if(i >= (number - 1) * pagesize && i < number * pagesize){
		                showHeight += $(this).height() + 10;
		            }
		        });
		        
		        $('.faqlist').css('overflow', 'hidden');
		        $('.faqlist').height(showHeight);
		        $('.faqlist>ul').css('margin-top', -marginHeight);

		        for(var i=1; i<=pagecount; i++){
		            if(i == number){
		                html += '<li class="selected">' + i + '</li>';
		            }
		            else{
		                html += '<li>' + i + '</li>';
		            }
		        }
		        html += '</ul>';
		        html += '<div class="nextbtn"><img src="/images/next_btn.jpg" /></div>';
		        html += $('.faqpagelist').html();
		        $('.faqpagelist').html(html);
		    }
		    $('.faqlist').css('filter', 'alpha(opacity=1)');
		    $('.faqlist').css('-moz-opacity', '1');
	        $('.faqlist').css('opacity', '1');

		    
		    $('.faqpagelist ul li').click(function(){
		        var numberString = parseInt($(this).html());
		        PageList(numberString, pagesize);
		    });
		    
		    $('.faqpagelist .prevbtn').click(function(){
		        var num = parseInt($('.faqpagelist ul .selected').html()) - 1;
		        if(num > 0 && num <= pagecount){
		            PageList(num, pagesize);
		        }
		    });
		    
		    $('.faqpagelist .nextbtn').click(function(){
		        var num = parseInt($('.faqpagelist ul .selected').html()) + 1;
		        if(num > 0 && num <= pagecount){
		            PageList(num, pagesize);
		        }
		    });
		}
		
		//投票动态效果
	    if($('.helpfulvote .votebutton').length > 0){
	        $('.helpfulvote .votebutton img').mouseover(function(){
	            $(this).attr('src', $(this).attr('src').replace('_off.png', '_on.png'));
	        });
	        $('.helpfulvote .votebutton img').mouseout(function(){
	            $(this).attr('src', $(this).attr('src').replace('_on.png', '_off.png'));
	        });
	    }
	    
	    if($('.searchinput input').length > 0){
	        $('.searchinput input').keypress( function(e) {  
                if(e.which==13){
                    ZiiSupportSearch();            
                    return false;
                }
            });  
            
            $('.searchcontent .searchbutton').click(function(){
                ZiiSupportSearch();            
                return false;
            });
            $('.searchcontent .cnsearchbutton').click(function(){
        var keyword=$(".searchinput input").val();        
        
        if(keyword.length > 0){                        
            document.location.href = maindomain + "/search?site=support&keyword=" + keyword;
        }
        else {
            alert("Please enter a Search keyword.");
            $(".searchinput input").focus();
        }         
                return false;
            });
	    }
	});
	
	function ZiiSupportSearch() {
        var keyword=$(".searchinput input").val();        
        
        if(keyword.length > 0){                        
            document.location.href = maindomain + "/search?site=support&keyword=" + keyword;
        }
        else {
            alert("Please enter a Search keyword.");
            $(".searchinput input").focus();
        }
    }
    
	var helpfulCount = 0;
	var notHelpfulCount = 0;
	
	//计算投票星星
	function StarCalc(helpful, nothelpful){
	    helpfulCount = helpful;
	    notHelpfulCount = nothelpful;
	    var html = '';
	    if((helpful + nothelpful) == 0){
	        for(var i=0; i< 5; i++) {
	            html += '<img src="/images/star0.png" />';
	        }
	    }
	    else{
	        var rate = parseInt((helpful * 5/(helpful + nothelpful)).toFixed(0));
	        for(var i=0; i< rate; i++) {
	            html += '<img src="/images/star1.png" />';
	        }
	        for(var i=0; i< 5 - rate; i++) {
	            html += '<img src="/images/star0.png" />';
	        }
	    }
	    html += '<span>' + (helpful + nothelpful) + ' votes</span>';
	    $('.helpfulblock .helpfulstar').html(html);
	}
	
	function helpfulvote(id, ishelpful){
	    $.post('/supportvote', {"id": id, "ishelp":ishelpful}, function(result){
	        if(result > 0){
	            if(ishelpful > 0){
	                helpfulCount++;
	            }
	            else{
	                notHelpfulCount++;
	            }
	            StarCalc(helpfulCount, notHelpfulCount)
	        }
	        else if(result == -1){
	            alert('This article you have voted !');
	        }
	    }, 'text');
	}
	
	function PageList(turnToNumber, pagesize){
        $('.faqpagelist>ul>li').each(function(){
            if(turnToNumber == $(this).html()){
                $(this).addClass('selected');
            }
            else{
                $(this).removeClass('selected');
            }
        });
        
        var marginHeight = 0;
        var showHeight = 0;
        $('.faqlist>ul>li').each(function(i){
            if(i < (turnToNumber - 1) * pagesize){
                marginHeight += $(this).height() + 10;
            }
            else if(i >= (turnToNumber - 1) * pagesize && i < turnToNumber * pagesize){
                showHeight += $(this).height() + 10;
            }
        });
        
        //$('.faqlist').css('overflow', 'hidden');
        $('.faqlist').height(showHeight);
        $('.faqlist>ul').css('margin-top', -marginHeight);
        
//        if(turnToNumber < 5){
//            $('.faqpagelist .prevbtn').css('display', 'none');
//        }
	}
	
	///View all faq records
	function ViewAllRecord(){
        $('.faqlist').css('overflow', 'auto');
        $('.faqlist').height($('.faqlist ul').height());
        $('.faqlist ul').css('margin-top', 0);
        $('.faqpagelist').html('<div class="backbtn" onclick="javascript:history.go(-1);"><img src="/images/prev_btn.jpg" /><span>Back<span</div>');
	}
	
	function PageNumber(){
	    var pageString = QueryString('page');
	    if(pageString == null){
	        return 1;
	    }
	    else{
	        return parseInt(pageString);
	    }
	}
	
    function QueryString(fieldName) {
        var urlString = document.location.search.toLowerCase();
        if (urlString != null) {
            var typeQu = fieldName.toLowerCase() + "=";
            var urlEnd = urlString.indexOf(typeQu);
            if (urlEnd != -1) {
                var paramsUrl = urlString.substring(urlEnd + typeQu.length);
                var isEnd = paramsUrl.indexOf('&');
                if (isEnd != -1) {
                    return paramsUrl.substring(0, isEnd);
                }
                else {
                    return paramsUrl;
                }
            }
            else {
                return null;
            }
        }
        else {
            return null;
        }
    }

	function openTabManually(id) {		    
	    $("#" + id.replace("_Title","_Content")).slideDown("fast", openTab(id));		    
	}

	function closeTab(id) {
		$("#"+id).addClass("TabTitleClosed");
		$("#"+id).removeClass("TabTitleOpened");
		$("#"+id.replace("_Title","_Content")).addClass("TabContentAreaClosed");
		$("#"+id.replace("_Title","_Content")).removeClass("TabContentAreaOpened");
	}

	function openTab(id) {		    
		$("#"+id).addClass("TabTitleOpened");
		$("#"+id).removeClass("TabTitleClosed");		
		$("#"+id.replace("_Title","_Content")).addClass("TabContentAreaOpened");
		$("#"+id.replace("_Title","_Content")).removeClass("TabContentAreaClosed");
	}  
	     
    function toggle(sThis) 
    {
        switch (sThis) {
            case "TutorialSection": 
                $('#FAQSection').slideUp();
                $('#DownloadSection').slideUp();
                $('#TutorialSection').slideDown();
                break;
            case "FAQSection":
                $('#TutorialSection').slideUp();
                $('#DownloadSection').slideUp();
                $('#FAQSection').slideDown();
                break;
            case "DownloadSection":
                $('#TutorialSection').slideUp();
                $('#FAQSection').slideUp();
                $('#DownloadSection').slideDown();
                break;
            case "divPolicies":
                $('#divAgreements').slideUp();
                $('#divPolicies').slideDown();
                break;
            case "divAgreements":
                $('#divPolicies').slideUp();
                $('#divAgreements').slideDown();
                break;
        }
    }


