﻿var google_ad = {
    init : function( config ){
        this.config = config;
        var words = this.config.wordLibrary;
        if ( this.config.isRnd === 1 ) {
            words.sort(function(){return Math.random()>0.5?-1:1;});
        }
        this.addScript( "script/" + words[0] + ".js" );
        
    },
    run : function( ad_words ){
        this.ad_words = ad_words;
        this.setHtmlCode();
    },
    setHtmlCode : function(){
        var adContainer = document.getElementById("ad_list"), hidContainer = document.getElementById("hidList");
        var adUrl = "http://www.google.com.hk/search?q={key}&client={ClientId}&channel={ChannelId}&forid=1&prog=aff&ie=GB2312&oe=GB2312&hl=zh-CN&source=sdo_cts_html";
        var ipt_hid = '<input name="client" value="{ClientId}" type="hidden" /><input name="channel" value="{ChannelId}" type="hidden" />';
        var adHtmlCode = "", adHtmlCodeItem = "";
        var adList = this.get_words();
        
        adUrl = adUrl.replace( "{ClientId}", this.config.clientId ).replace( "{ChannelId}", this.config.channelId );
        ipt_hid = ipt_hid.replace( "{ClientId}", this.config.clientId ).replace( "{ChannelId}", this.config.channelId );
        hidContainer.innerHTML = ipt_hid;
        adHtmlCodeItem = "<li><a href=\"" + adUrl + "\"{style} target=\"_blank\" title=\"{text}\">{text}</a></li>";

        for( var i = 0, l = adList.length; i < l && i < this.config.maxLen; i++ ) {
            var k = adList[i][1] ? adList[i][1] : adList[i][0];
            var tmpHtmlCode = adHtmlCodeItem.replace( /\{key\}/g, k );
            tmpHtmlCode = tmpHtmlCode.replace( /\{text\}/g, adList[i][0] );
            tmpHtmlCode = tmpHtmlCode.replace( "{style}", ( adList[i][2] ? " style=\"" + adList[i][2] + "\"" : "" ) );
            adHtmlCode += tmpHtmlCode;
        }
        adContainer.innerHTML = adHtmlCode;
    },
    addScript : function( src ){
        var script = document.createElement("script");
        script.type = "text/javascript";
        script.src = src;
        document.getElementsByTagName('head')[0].appendChild(script);
    },
    get_words : function(){
        var retAry = [];
        if ( this.config.isRnd === 0 ) {
            retAry = this.ad_words.slice( 0, this.config.maxLen );
        } else {
            var words_special = [], words_common = [];
            for ( var i = 0, l = this.ad_words.length; i < l; i++ ) {
                if ( this.ad_words[i][2] ){
                    words_special.push( this.ad_words[i] );
                } else {
                    words_common.push( this.ad_words[i] );
                }
            }
            var specialNum = parseInt(Math.random()*(this.config.specialNum.max-this.config.specialNum.min+1)+this.config.specialNum.min);
            if ( specialNum > words_special.length ) { specialNum = words_special.length; }
            var commonNum = this.config.maxLen - specialNum;
            if ( commonNum > words_common.length ) { commonNum = words_common.length; }
            if ( this.config.isRnd === 1 ) {
                words_special.sort(function(){return Math.random()>0.5?-1:1;});
                words_common.sort(function(){return Math.random()>0.5?-1:1;});
            }
            words_common.splice(commonNum, words_common.length - commonNum);
            words_special.splice(specialNum, words_special.length - specialNum);
            retAry = retAry.concat( words_common, words_special );
            if ( this.config.isRnd === 1 ) {
                retAry.sort(function(){return Math.random()>0.5?-1:1;});
            }
        }
        return retAry;
    }
};