var leftpos=[]; 
var RIGHTPAD = 10, TOPPAD = 25, LEFTPAD = 10, BOTPAD = 25, BOXW = 170, BOXH = 370, ENDPAD = 30, pageW = 0, cols = 0, maxcols = 5, sizeMsg = false;

var twitter_url	=	'http://twitter.com/statuses/user_timeline/223382547.rss?count=1';
var sinal_url	=	'http://api.t.sina.com.cn/statuses/user_timeline/1882028730.xml?source=1785281794&count=1';

//var twitter_url	=	'http://pondoka.twbbs.org/donna_hsu/218009546.json';
$(document).ready(
	function(){
		
		load_sina();
		loadTwitter();
		load_facebook();
		if($('.social_box').is('div')){
//				load_fb();
		}		
		$('#nav_control_div').click(
			function(){
				if ($("#nav_show_div").css('display') == 'none') {
					show_quick_nav();
				}
				else {
					hide_quick_nav();
				}
				
			}
		);
		initial_ele();
		initial_popup();
		initial_show_box();
	});
	
function hide_quick_nav(){
	//需要多語系
	var label = '快速導覽';
	if(lang == '_ch'){
		label 	=  '快速导览';
	}	
	else if(lang=='_en'){
		label 	=	'Quick link';
	}
	$('#nav_control_div').attr('title','open navigetion');
	$('#nav_control_div').html(label+' <img class="icons_01" src="css/css_img/hold.gif" />');
	$("#nav_show_div").fadeOut("slow");		
}

function show_quick_nav(){
	//需要多語系
	var label = '快速導覽';
	if(lang == '_ch'){
		label 	=  '快速导览';
	}
	else if(lang=='_en'){
		label 	=	'Quick link';
	}	
	$('#nav_control_div').attr('title','open navigetion');
	$('#nav_control_div').html(label+' <img class="icons_01" src="css/css_img/hold.gif" />');
	$("#nav_show_div").fadeIn("slow");	
}	
	
$(window).load(function(){
	reposition();
	setHeader();
	setFooter();

    $(window).resize(function(){
        window.setTimeout("reposition()", 200);
		window.setTimeout("setFooter()", 200);
		window.setTimeout("setHeader()", 200);
		window.setTimeout("resize_showbox()", 200);
    });
});

function getsina_date(time_value){
	var values = time_value.split(" ");
	
	time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
	var parsed_date = Date.parse(time_value);
	var post_date = new Date(parsed_date);
	time_value	=	post_date.format('yyyy-mm-dd HH:MM:ss');
//	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
//	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
//	if (values[5] == "+0000") {
//		delta = delta + (relative_to.getTimezoneOffset() * 60);
//	} else {
//		delta = delta + relative_to.getTimezoneOffset();
//	}
	
	
	return time_value;	
	
	
}

// pubDate delta function
function get_date(time_value) {
	
	var values = time_value.split(" ");
	time_value = values[2] + " " + values[1] + ", " + values[3] + " " + values[4];
	var parsed_date = Date.parse(time_value);
	var post_date = new Date(parsed_date);
	time_value	=	post_date.format('yyyy-mm-dd HH:MM:ss');
//	var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
//	var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
//	if (values[5] == "+0000") {
//		delta = delta + (relative_to.getTimezoneOffset() * 60);
//	} else {
//		delta = delta + relative_to.getTimezoneOffset();
//	}
	
	
	return time_value;
}

Array.max = function(array){
   		 return Math.max.apply( Math, array );
		}; 
//
function setHeader(){
		maxLeft=Array.max(leftpos);
		if(maxLeft<550){
			$('#header_container').css('width',550);
			}
		else{
			$('#header_container').css('width',maxLeft+BOXW);
			}	
		
			$('#footer_container').css('width',maxLeft+BOXW);
		leftpos=[];
}
function setFooter(){
		$('#footer_container').css('top',0);
		$('#footer_container').css('top',$(document).height()-500);
}


function reposition(f){
    pid = f;
    CARDBORDER = 30;
    this.col_ar = [];
    this.init = function(){
		//找出box_container裡有幾個div
        var a = document.getElementById("box_container").childNodes.length;
        this.pushed_ar = [];
		//取得頁面的寬度
        var b = this.getPageW();
		//計算 
        for (c = cols = Math.floor((b - LEFTPAD * 2) / (BOXW + RIGHTPAD)); c > 0; c--)
		// cols : 目前頁面寬度可容納的行數
		//  若是 c 小於等於 目前寬度 - 3
            if (c <= cols - 3) {
                targetSelCol = c;
                break;
            }
        for (c = 0; c < a; c++) {
			//找出 box_container 所有元件 並且將 b 的 id 放入陣列中
            b = document.getElementById("box_container").childNodes[c];
            $(b).attr("tagName") == "DIV" && this.pushed_ar.push(b.id)
        }
		//產生行陣列
        this.col_ar = [];
        if (!cols || cols < 3) 
            cols = 3;
        this.col_ar.push({
            x: 0,
            y: 0
        });
        for (var c = 1; c < cols; c++) 
            this.col_ar.push({
                x: c,
                y: 0
            });
        this.draw()
    };
	//取得當前頁面寬度
    this.getPageW = function(){
        var a;
        if (self.innerWidth) 
            a = self.innerWidth - 25;
        else 
            if (document.documentElement && document.documentElement.clientWidth) 
                a = document.documentElement.clientWidth - 25;
            else 
                if (document.body) 
                    a = document.body.clientWidth - 25;
        return a
    };
	//取得物件的offestHeight
    this.getHeight = function(a){
        return document.getElementById(a).offsetHeight
    };
    this.draw = function(){
        var a = pushed_ar;
        for (var b in a) {
            b = a[b];
            this.col_ar.sort(this.ySort);
            var c = document.getElementById(b);
            if (c) 
                if (Math.round(c.offsetWidth / BOXW) > 1) 
                    this.drawWide(b);
                else {
                    c = this.col_ar[0];
                    this.drawItem(b, c.x, c.y)
                }
        }
    };
    this.drawWide = function(a){
        var b = [];
        this.col_ar.sort(this.xSort);
        var c = document.getElementById(a);
        c = Math.round(c.offsetWidth / BOXW);
        var e = cols - c;
        if (!(e < 0)) {
            if (e < 0) {
                e = 1;
                c = 2
            }
            for (var d = 0; d <= e; d++) {
                b.push(this.col_ar[d]);
                for (var j = 0, k = null, g = 0; g < c; g++) {
                    var h = this.col_ar[d + g].y;
                    if (h >= j) {
                        j = h;
                        k = d + g
                    }
                }
                h = j;
                for (g = 0; g < c; g++) 
                    h += Math.abs(this.col_ar[k].y - this.col_ar[d + g].y);
                b[d].d = h;
                b[d].od = b[d].y - j
            }
            b.sort(this.dSort);
            c = b[0].x;
            e = b[0].y;
            if (b[0].od < 0) 
                e -= b[0].od;
            this.col_ar.sort(this.ySort);
            this.drawItem(a, c, e)
        }
    };
    this.xSort = function(a, b){
        return a.x - b.x
    };
    this.ySort = function(a, b){
        return a.y == b.y ? a.x - b.x : a.y - b.y
    };
    this.dSort = function(a, b){
        return a.d == b.d ? a.x - b.x : a.d - b.d
    };
    this.drawItem = function(a, b, c){
        a = document.getElementById(a);
        var e = Math.round(a.offsetWidth / BOXW);
        wleftArr = b * (BOXW + RIGHTPAD) + LEFTPAD;
        a.style.left = wleftArr + "px";
        leftpos.push(wleftArr);
        a.style.top = c + TOPPAD + "px";
        a.style.position = "absolute";
        a.style.visibility = "visible";
        if (e > 1) {
            this.col_ar.sort(this.xSort);
            for (var d = 0; d < e; d++) 
                this.col_ar[b + d].y = c + this.getHeight(a.id) + BOTPAD
        }
        else 
            this.col_ar[0].y = c + this.getHeight(a.id) + BOTPAD
    };
    this.findSameTop = function(a, b){
        return a == b ? true : false
    };
    this.findProximity = function(a, b, c){
        return c >= a && c <= b ? true : false
    };
    this.findClosest = function(a, b){
        return a >= b ? a : b
    };
    this.findSameCol = function(a, b, c, e){
        return c == e ? a == b ? true : false : false
    };
    this.init(pid)
}

function getScrollHeight(){
    var f;
    if (self.pageYOffset) 
        f = self.pageYOffset;
    else 
        if (document.documentElement && document.documentElement.scrollTop) 
            f = document.documentElement.scrollTop;
        else 
            if (document.body) 
                f = document.body.scrollTop;
    return parseInt(f) + _getWindowHeight()
}

function _getWindowHeight(){
    if (self.innerWidth) {
        frameWidth = self.innerWidth;
        frameHeight = self.innerHeight
    }
    else 
        if (document.documentElement && document.documentElement.clientWidth) {
            frameWidth = document.documentElement.clientWidth;
            frameHeight = document.documentElement.clientHeight
        }
        else 
            if (document.body) {
                frameWidth = document.body.clientWidth;
                frameHeight = document.body.clientHeight
            }
    return parseInt(frameHeight)
}

function arfind(f, a){
    for (i = 0; i < f.length; i++) 
        if (f[i] == a) {
            spot = i;
            break
        }
    return spot
};

