function getImageInformation(pageNo){
    pageNo  =   filterPageNo(pageNo);
    if(leftFolding == true){
        resetPreviousFolding();
        leftFolding = false;
    }
    if(rightFolding == true){
        resetNextFolding();
        rightFolding = false;
    }
    getPagination(pageNo);
    jsImagePopUPHide();//suspended by sdt
    flipToPage(pageNo);
}
function getPagination(pageNo){
    $.post(siteUrl+"ajaxrequest.php",
    {
        page: pageNo,
        total_pages : numberOfPages-1,
        magazine_name : magazineName
    },
    function(transport) {
        $('#dynamicPaging').html(transport);
        setShowAll();
        setSendToFriend();
    }
    );
}
function showNextImage(){
    pageNo = getNextPageNumber();
    if(!pageNo && pageNo != 0) return;
    
    getImageInformation(pageNo);
    jsImagePopUPHide();
}
function showPreviousImage(){
    pageNo = getPreviousPageNumber();
    if(!pageNo && pageNo!=0) return;

    getImageInformation(pageNo);
    jsImagePopUPHide();
}
function showCoverPage(){
    getImageInformation(0);
}
function showBackPage(){
    getImageInformation(numberOfPages-1);
}
function getNextPageNumber(){
    if(typeof currentPage == "undefined" || currentPage == null || currentPage == 0 || currentPage < 0){
        pageNo = 1;
    }else if(currentPage>0 && currentPage < (numberOfPages-1)){
        pageNo = currentPage+2;
    }else if(currentPage >= numberOfPages-1) {
        pageNo=0;
    }else return false;
    loadThumbImageByNo(pageNo);
    return pageNo;
}

function getPreviousPageNumber(){
    if(typeof currentPage == "undefined" || currentPage == null || currentPage == 0){
        pageNo = numberOfPages-1;                 //when the current page is Front page
    }else if(currentPage==1){
        pageNo = 0;
    }else if(currentPage>1){
        pageNo = currentPage-2;
    }else if(currentPage==-1){     //when the current page is Back page
        pageNo = numberOfPages-1;
    }else 
        return false;
    
    loadThumbImageByNo(pageNo);
    return pageNo;
}
/**  go to any page  ***/
function goToPage(objectInput){
    InputValue   =   $(objectInput).val().toLowerCase();
    $(objectInput).select();
    filterGoToPage(InputValue);
}
function filterGoToPage(InputValue){
   if(isNumeric(InputValue)){
        InputValue =Number(InputValue);
        if(InputValue == 0 & InputValue != currentPage){
            return getImageInformation(0);
        }else if(InputValue == currentPage){
            return false;
        }
        getImageInformation(InputValue);
    }
    else{
        if( ( InputValue.match("c1")   && currentPage == 0) || (InputValue.match("c2") && currentPage == 1)){
            return false;
        }else if( ( InputValue.match("c4")   && currentPage == numberOfPages-1)  ||   (InputValue.match("c3") && currentPage == numberOfPages-3) ){
            return false;
        }else if(InputValue.match("c1")  ){
           getImageInformation(0);
        }else if( InputValue.match("c2")){
           getImageInformation(1);
       }else if((InputValue.match("c3") ) ){
           getImageInformation(numberOfPages-3);
        }else if(InputValue.match("c4")  ){
           getImageInformation(numberOfPages-1);
        }else
            return false;
    }    
}


function goToPageByEnter(objectInput) {
    $(objectInput).keyup(function(e) {
        if(e.keyCode == 13) {
            goToPage(objectInput);
        }
    });
}
/**
 * document on load
 */
$(document).ready(function() {
    setShowAll();
    setSendToFriend();
});

/**
 * Make the show all link working
 */
function setShowAll() {
    $("a.show_all").fancybox({
        'overlayOpacity'	:	0.7,
        'overlayColor'		:	'#404040'
    });
    $("#fancy_close").click();
}

/**
 * Navigate the show all box
 * @param   max_col     ineteger    maximum column per row
 * @param   start_file  ineteger    start file number
 * @param   end_file    ineteger    end file number
 * @param   total_pages ineteger    number of pages in the magazine
 * @param   work        boolean     should the navigation work or not?
 */
function navigateShowAll(page_no, total_items, magazine_name) {
    $.get(siteUrl+ "showall.php",
    {
        page_no         : page_no,
        total_items     : total_items,
        magazine_name   : magazine_name
    },
    function(transport) {
        $('#fancy_ajax').html(transport);
    }
    );
}
function setSendToFriend() {
/*    $("a.greybox").click(function(){
        var t = this.title || $(this).text() || this.href;
        GB_show(t,this.href,560,532);
        return false;
    });*/
    $("a.send_to_friend").fancybox_send_to_friend({
        'overlayOpacity'	:	0.7,
        'overlayColor'		:	'#404040'
    });
    $("#fancy_close").click();
}
function isNumeric(inputValue){//@field_id,@min_length,@max_lem
    var reg = /^[+-]?\d*\.?\d+$/i
    if(testRegexp(inputValue,reg)){
        return 1;
    }
    return 0;
}
function testRegexp(str,regexp) {
    if ( !( regexp.test( str ) ) )return false;
    return true;
}

function send_to_friend() {
    from_email  = $('#from_email').val();
    to_email    = $('#to_email').val();
    message     = $('#message').val();
    link_path   = $('#link_path').val();
    left_path   = $('#left_path').val();
    right_path   = $('#right_path').val();
    $.post(
        siteUrl + 'includes/sendtofriend.php',
        {
            from_email   : from_email,
            to_email     : to_email,
            message      : message,
            link_path    : link_path,
            left_path    : left_path,
            megazine_id  : magazineName,
			current_page : currentPage,
            right_path   : right_path
        },
        function(transport) {
            $('#send_to_friend_container').html(transport);
        }
    );
}

