/**
 * 「今日」日期字串
 * 暫時提供二款格式
 * 
 * @param   string  space   間隔符號
 * @return  string  today
 */
function getTodayStr(space){
    if(typeof(space)!='string') space = '';
    var d = new Date(); var mm = d.getMonth()+1; var dd = d.getDate();
    var today = d.getFullYear()+space+((mm>=10)?mm:'0'+mm)+space+((dd>=10)?dd:'0'+dd);
    
    return today;
}
var today_str1 = getTodayStr();//20100513
var today_str2 = getTodayStr('-');//2010-05-13

/**
 * 製作出_trackPageview的value, 並且執行GA js
 * 
 * @param   string  page    必填; 哪一頁, maybe: 首頁, 活動頁, 主辦單位頁 ...
 * @param   string  item    必填; 哪個元素, maybe: event, venues, something...
 * @param   string  medium  某個版位(版塊), maybe: 廣告190x160, flashbanner, 訂閱電子報
 * @param   string  type    用什麼型式展現? 文字? 圖片? 影音?, maybe: mv, img, str, flash
 * @param   string  settime 是否要加入時間 today_str1 / today_str2 / ''
 * @param   string  rtn     'true' 回傳字串+不執行GA js / 'false' 不回傳字串+執行GA js
 * @return  string  str     只在rtn='true'才回傳
 */
function setGAtrackPageview(page, item, medium, type, settime, rtn){
    if(typeof(rtn)!='string') var rtn = 'false';
    else rtn = (rtn=='true')? 'true': 'false';
    if(typeof(page)!='string') return false;
    if(typeof(item)!='string') return false;
    if(typeof(medium)!='string') var medium = '';
    if(typeof(type)!='string') var type = '';
    if(typeof(settime)!='string') var settime = '';
    else {
        if(typeof(eval(settime))!='string') var settime = '';
        else var settime = eval(settime);
    }
    if(settime=='true'){
        var str = '/'+page+'/'+today_str1+((medium=='')?'':'/'+medium)+'/'+item+((type=='')?'':'/'+type)+'.html';
    }
    else{
        var str = '/'+page+((medium=='')?'':'/'+medium)+'/'+item+((type=='')?'':'/'+type)+'.html';
    }
    
    try{
        //alert(str);//test
        if(rtn=='true') return str;
        else pageTracker._trackPageview(str);
    }
    catch(e){
        try{ // 精選那些可能是用這個變數名
            //alert(str);//test
            firstTracker._trackPageview(str);
        }
        catch(e){}
    }
    /*
    try{
        console.log('pageTracker._trackPageview：%d',str);
    }
    catch(e){}
    */
}

/**
 * 記錄GA click, 規格做個統一用的
 * 
 * 順位: 1.title, 2.alt
 * 
 * @param   string  attrb   html屬性, 預設為'title', 做為 option.item的值; 抓取<a title="abc">text1</a>, 得 'abc'
 * @param   string  item    option.item, 為空字串時, 會自動抓取attrb, 如果還是空字串, 則會抓取 $(this).html();
 * 其它參數, 請參照 function setGAtrackPageview(page, item, medium, type, settime)
 *
 * sample: 
 *        var option={page:'', item:'', medium:'', type:'', settime:'', ignoreID:'', attrb:'title'};
 *        $('a').setClick({page:'首頁', item:'', ignoreID:'btnnext'});
 */
jQuery.fn.setClick = function(option){
    if( (typeof(this)!='object') || (this.size()==0) ) return false;
    var opt = $.extend({page:'', item:'', medium:'', type:'', settime:'', ignoreID:'', attrb:'title', rtn:''}, option);
    $(this).click(function(){
        try{
            //按指定的屬性抓值
            if(opt.item=='') opt.item = $(this).attr(opt.attrb);
                if(typeof(opt.item)!='string') opt.item='';
            //略過不處理的項目
            if(opt.ignoreID!='' && $(this).attr('id')==opt.ignoreID) return true;
            
            //試著找title, alt
            if(opt.item.length<=0 && typeof($(this).attr('title'))=='string') opt.item = $(this).attr('title');
                if(typeof(opt.item)!='string') opt.item='';
            if(opt.item.length<=0 && typeof($(this).attr('alt'))=='string') opt.item = $(this).attr('alt');
                if(typeof(opt.item)!='string') opt.item='';
            
            //試著找包含物件的其它屬性
            var tagname = $(this).attr('tagName');
            //<a>
            if(opt.item.length<=0 && tagname=='A'){
                //<img>
                if(typeof($(this).find('img').attr('src'))=='string'){
                    if(opt.item.length<=0 && typeof($(this).find('img').attr('title'))=='string') opt.item = $(this).find('img').attr('title');
                        if(typeof(opt.item)!='string') opt.item='';
                    if(opt.item.length<=0 && typeof($(this).find('img').attr('alt'))=='string') opt.item = $(this).find('img').attr('alt');
                        if(typeof(opt.item)!='string') opt.item='';
                    if(opt.item.length<=0) opt.item='image';
                }
                //<span>
                if(opt.item.length<=0 && typeof($(this).find('span').html()=='string')){
                    opt.item = $(this).find('span').html();
                    if(typeof(opt.item)!='string') opt.item='';
                }
            }
            //<object>
            if(opt.item.length<=0 && tagname=='OBJECT'){
                if(typeof($(this).find('embed').attr('src'))=='string'){
                    if(opt.item.length<=0 && typeof($(this).find('embed').attr('title'))=='string') opt.item = $(this).find('embed').attr('title');
                        if(typeof(opt.item)!='string') opt.item='';
                    if(opt.item.length<=0 && typeof($(this).find('embed').attr('alt'))=='string') opt.item = $(this).find('embed').attr('alt');
                        if(typeof(opt.item)!='string') opt.item='';
                }
                if(opt.item.length<=0) opt.item='flash';
            }
            //<embed>
            if(opt.item.length<=0 && tagname=='EMBED'){
                opt.item='flash';
            }
            
            //什麼都沒有的時候, 試看看直接抓內容
            if(opt.item.length<=0) opt.item = $(this).html();
                if(typeof(opt.item)!='string') opt.item='';
            //alert('type:'+typeof(opt.item)+', len:'+opt.item.length+', val:'+opt.item);//test
            
            setGAtrackPageview(opt.page, opt.item, opt.medium, opt.type, opt.settime, opt.rtn);
        }
        catch(e){}
    });
    
    return false;
};

/**
 * 啟用GA click
 *
 * 啟動事件設在 $(window).load();
 *
 */
$(window).load(function(){
    if(typeof(option)=='object' && typeof(option.page)=='string'){
        try{
            option.medium='';
            //for all html dom: a
            $('a').each(function(){
                $(this).setClick(option);
            });

            $('input').each(function(){
                $(this).setClick(option);
            });
			
            //for all object: flash/mv
            $('object').each(function(){
                $(this).setClick(option);
            });
            //for openX ads
            option.medium='OpenX';
            $('iframe.banner').contents().find('a').each(function(){
                $(this).setClick(option);
            });
            $('iframe.banner').contents().find('embed').each(function(){
                $(this).setClick(option);
            });
            //option.XXX: 有設定過其它內容的話, 記得要弄回預設再使用
            option.medium='';
        }
        catch(e){}
    }
});


