function str_replace(haystack, needle, replacement) {
    var temp = haystack.split(needle);
    return temp.join(replacement);
}

function greyedText(){
	$('input.greyed').each(function(){
		if($(this).val() == ''){
			$(this).val($(this).attr('alt'));
			$(this).addClass('greyed-text');
		}
		
		$(this).focus(function(){
			if($(this).val() == $(this).attr('alt')){
				$(this).val('');
				$(this).removeClass('greyed-text');
			}
		});
		$(this).blur(function(){
			if($(this).val() == ''){
				if(!$(this).hasClass('remain'))
					$(this).val($(this).attr('alt'));
				$(this).addClass('greyed-text');
			}
		});
	});
}

function generatePageNumber(totalRecord, currentPage, recordPerPage, pageNumberShowCount, callback, callbackParam){
	totalPage = parseInt(totalRecord) / parseInt(recordPerPage);
	temp = new String(totalPage);
	if(temp.indexOf('.') > -1){
		totalPage2 = temp.substr(0, temp.indexOf('.')); //Math.round(totalPage);
	}else{
		totalPage2 = totalPage;
	}
	totalPage2 = parseInt(totalPage2);
    
    if(totalPage != totalPage2){
    	totalPage = totalPage2 + 1;
    }
    //alert(totalPage+' - '+totalPage2+' - '+totalRecord+' - '+recordPerPage);
	pageNumberList = generatePageNumberList(totalPage, currentPage, recordPerPage, pageNumberShowCount);	
	pagingInnerContainer = $('<ul></ul>');
	if(currentPage > 1){
		$('<li></li>').addClass('arrow').text('<').click(function(){
			callback.clickAction((parseInt(currentPage) - 1), callbackParam);
			//redirect('browse', {subcat: currentSubCatId, page: currentPage - 1});
		}).appendTo(pagingInnerContainer);
	}
	for(i=0; i<pageNumberList.length; i++){
		pagingItemContainer = $('<li></li>');
		pagingItemContainer.text(pageNumberList[i].pageNumber).click(function(){
			callback.clickAction($(this).text());
			//redirect('browse', {subcat: currentSubCatId, page: $(this).text()});
		});
		if(pageNumberList[i].isCurrentPage){
			pagingItemContainer.addClass('selected').appendTo(pagingInnerContainer);
		}else{
			pagingItemContainer.appendTo(pagingInnerContainer);
		}
	}
	if(pageNumberList.length > 0 && totalPage > pageNumberList[pageNumberList.length-1].pageNumber){
		$('<li></li>').text('... '+totalPage).click(function(){
			callback.clickAction(totalPage);
			//redirect('browse', {subcat: currentSubCatId, page: totalPage});
		}).appendTo(pagingInnerContainer);
	}
	if(currentPage < pageNumberList.length){
		$('<li></li>').addClass('arrow').text('>').click(function(){
			callback.clickAction((parseInt(currentPage) + 1));
			//redirect('browse', {subcat: currentSubCatId, page: currentPage + 1});
		}).appendTo(pagingInnerContainer);
	}
	$('div class="clearfix"></div>').appendTo(pagingInnerContainer);
	
	return pagingInnerContainer;
}
function generatePageNumberList(totalPage, currentPage, recordPerPage, pageNumberShowCount){
	if(currentPage + (pageNumberShowCount-1) > totalPage){
        startPos = (totalPage - currentPage + 1) < pageNumberShowCount ? (totalPage - pageNumberShowCount + 1) : currentPage;
        if(startPos < 1){
        	startPos = 1;
        }
    }else{
        startPos = currentPage;
    }
    if(totalPage < pageNumberShowCount){
        endPos = pageNumberShowCount;
    }else{
        endPos = parseInt(currentPage) + (parseInt(pageNumberShowCount) - 1);
    }
    output = new Array();
    count = 0;
    for(i=startPos; i<=endPos; i++){
    	pagingItemContainer = $('<li></li>');
    	
        if(i <= totalPage){
            if(i == currentPage){
                output[count] = {pageNumber: i, isCurrentPage: true};
            }else{
                output[count] = {pageNumber: i, isCurrentPage: false};
            }
            count++;
        }else{
        
        }
    }
    
    return output;
}
