var WISH_BIG = {};
WISH_BIG.rootpath = 'cgi/';
WISH_BIG.wishbigEmptyText = 'Please note if you are a resident of the excluded regions of NY, FL, RI or Quebec, your entry will not be valid.';
WISH_BIG.focusTextVal = function(){
	var obj = document.getElementById('WishBigText');
	if(obj.value == WISH_BIG.wishbigEmptyText){
		obj.value = '';
		obj.style.color = '#000';
	}
};
WISH_BIG.blurTextVal = function(){
	var obj = document.getElementById('WishBigText');
	if(obj.value.length == 0){
		obj.value = WISH_BIG.wishbigEmptyText;
		obj.style.color = '#666';
	}
};
WISH_BIG.submitToggle = 0;
WISH_BIG.submit = function(err_id){
	if(WISH_BIG.submitToggle){
		alert('Your wish is being submitted.');
		return;
	}
	WISH_BIG.submitToggle = 1;
	var token = T.GetToken();
	if(!token){
		WISH_BIG.submitToggle = 0;
		alert('You must be logged in to submit your wish.  Please log in at the top of this page and try again.  If you do not have a THQ*ICE Passport, click Register Now to get your free account.');
		return;
	}
	var content = $('#WishBigText').val();
	if(WISH_BIG.wishbigEmptyText == content || content.length<6 || content.length>100){
		WISH_BIG.submitToggle = 0;
		alert('Your wish must contain between 6 and 100 characters.');
		return;
	}
	$.ajax({
        type: "POST",
        url: WISH_BIG.rootpath + "new_wish.php",
        dataType: 'json',
        data: {
        	token: token
        ,	content : content
        },
        timeout: 30000,
        success: function(json){
			WISH_BIG.submitToggle = 0;
            if (json.code === RETURN_CODE.SUCCESS) {
            	alert('Success!');
            	redirect('success.html');
            }
            else {
            	if(json.msg == -1){
            		T.error(err_id, 'Your session timed out. Please log in again.');
            	}else{
                	T.error(err_id, json.msg);
            	}
            }
        },
		error:function(){
			WISH_BIG.submitToggle = 0;
			T.error(err_id, getLang('http_request_faild'));
		}
	});	
};
WISH_BIG.getFansNumb = function(){
	$.ajax({
		type: "POST",
        url: WISH_BIG.rootpath + "wish_info.php",
        dataType: 'json',
        data: '',
        timeout: 30000,
        success: function(json){
			if (json.code === RETURN_CODE.SUCCESS) {
            	$('#facebook_fans').empty();
            	$('#facebook_fans').append(json.number_facebook);
            	$('#twitter_fans').empty();
            	$('#twitter_fans').append(json.number_twitter);
            	$('#current_supporters').empty();
            	$('#current_supporters').append(Math.floor(json.number_twitter) + Math.floor(json.number_facebook));
            }
        }
	});
};
WISH_BIG.List = {
	curr_page: 1
,	prev_page: 1
,	next_page: 1
,	total_pages: 1
,	list: []
};
WISH_BIG.getWishList = function(){
	var err_id = 'WishTableError';
	$.ajax({
        type: "POST",
        url: WISH_BIG.rootpath + "wish_list.php",
        dataType: 'json',
        data: {
        	page : WISH_BIG.List.curr_page
        },
        timeout: 30000,
        success: function(json){
            if (json.code === RETURN_CODE.SUCCESS) {
            	WISH_BIG.List.curr_page = json.curr_page;
            	WISH_BIG.List.prev_page = json.prev_page;
            	WISH_BIG.List.next_page = json.next_page;
            	WISH_BIG.List.total_pages = json.total_pages;
            	WISH_BIG.List.list = json.list;
            	var tbl = '', len = json.list.length, i =0, pgs = '';
            	if(len == 0){
            		tbl = "EMPTY";
            	}else{
            		tbl += '<tr class="wish_table_header td1"><th style="width:100px;" class="td1">Name</th><th class="td1">Wish</th></tr>';
            		for(; i < len; i++){
            			tbl += '<tr class="td2"><td>' + json.list[i].User + '</td><td>' + json.list[i].Content + '</td></tr>';
            		}
            		tbl = '<table width="534" border="0" cellspacing="1" cellpadding="1">' + tbl + '</table>';
            	}
            	$('#WishTable').html(tbl);
				if(json.total_pages > 10){
					var page_low = json.curr_page - 1;
					var page_high = json.curr_page + 1;
					if(json.curr_page <= 4){
						for(i=1; i<=page_high; i++){
							if(json.curr_page == i){
								pgs += i+'&nbsp;';
							}else{
								pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
							}
						}
						pgs += '...&nbsp;';
						for(i=json.total_pages -1; i<=json.total_pages; i++){
							pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
						}
					}else if(json.curr_page >= json.total_pages -3){
						for(i=1; i<=2; i++){
							pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
						}
						pgs += '...&nbsp;';
						for(i=page_low; i<=json.total_pages; i++){
							if(json.curr_page == i){
								pgs += i+'&nbsp;';
							}else{
								pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
							}
						}
					}else{
						for(i=1; i<=2; i++){
							pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
						}
						pgs += '...&nbsp;';
						for(i=page_low; i<=page_high; i++){
							if(json.curr_page == i){
								pgs += i+'&nbsp;';
							}else{
								pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
							}
						}
						pgs += '...&nbsp;';
						for(i=json.total_pages -1; i<=json.total_pages; i++){
							pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
						}
					}
				}else{
					for(i=1; i<=json.total_pages; i++){
						if(json.curr_page == i){
							pgs += i+'&nbsp;';
						}else{
							pgs += '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.gotoPage('+i+');">'+i+'</a>&nbsp;';
						}
					}
				}
				
				pgs = '<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.prevPage();">Prev Page</a>&lt;&nbsp;'+ pgs +'&gt;<a href="#WishBigListAnchor" onclick="javascript:WISH_BIG.nextPage();">Next Page</a>';
				$('#WishTablePages').html(pgs);
            }
            else {
            	if(json.msg == -1){
            		T.error(err_id, 'Your session timed out. Please log in again.');
            	}else{
                	T.error(err_id, json.msg);
            	}
            }
        },
		error:function(){
			T.error(err_id, getLang('http_request_faild'));
		}
	});
};
WISH_BIG.prevPage = function(){
	WISH_BIG.List.curr_page = WISH_BIG.List.prev_page;
	WISH_BIG.getWishList();
};
WISH_BIG.nextPage = function(){
	WISH_BIG.List.curr_page = WISH_BIG.List.next_page;
	WISH_BIG.getWishList();
};
WISH_BIG.gotoPage = function( idx ){
	WISH_BIG.List.curr_page = idx;
	WISH_BIG.getWishList();
};