﻿// NEED TO add bogus hash lookup
// NEED TO REDIRECT hash in CODEBEHIND (or just load based on it)
// BROWSE arrows need to be hooked to ajax version
// CHECK and FIX browse file numbering whenever changed/loaded


// ******************** PAGE LOAD ********************

//  <li class='p1'>
//    <a href='default.aspx?id=faith' class='faith'>
//      <img src='../images/photo_thumbs/faith.jpg' alt='faith.jpg'/>
//    </a>
//  </li>

//  <li class='p1'>
//    <a href='default.aspx?id=faith' class='faith active'>
//      <img src='../images/photo_thumbs/faith.jpg' alt='faith.jpg'/>
//    </a>
//  </li>

$(document).ready(function () {

    //$.url.setUrl("http://www.flickr.com/");
    // ajax requests

    var _pageName = $.url.param("id");
    var _pageAnchor = $.url.attr("anchor");
    var _pageDefault = "faith";

    //alert(_pageAnchor);

    if ((_pageAnchor != null) && (_pageAnchor != undefined)) {
        _pageName = _pageAnchor;
        fnAjaxRefresh(_pageName);
    } else {
        if (_pageName == undefined) {
            _pageName = _pageDefault;
        } else if (_pageName == "") {
            _pageName = _pageDefault;
        }
    }


    // thumbs initital stuff

    var _i, _n, _strLastP;                         // creat thumb page nav

    _strLastP = $('.thumbs ul li').last().attr('class');

    _n = 1 * _strLastP.split("p")[1]; // "1 *" makes sure it's interpreted as a number

    for (_i = 1; _i <= _n; _i++) {
        $('.thumbNav').append("<a href='' class='p" + _i + "'>" + _i + '</a>');
    }

    //$('.thumbNav li.p1').addClass('active');

    var _liSelected = $('div.thumbs li a.' + _pageName).parent('li');
    var _thumbPage = _liSelected.attr('class');  //needs to be before 'active' added

    _liSelected.addClass('active');
    $('.thumbNav a.' + _thumbPage).addClass('active');
    $('div.thumbs li').css('display', 'none');                      // hide thumbs (done init in CSS too; maybe redundant)
    $('div.thumbs li.' + _thumbPage + ' img').each(function (index) {
            $(this).attr('src', $(this).attr('title'));
            $(this).attr('title', '')
    });

    $('div.thumbs li.' + _thumbPage).css('display', 'block');

    $('.thumbs li.active').fadeTo(0, 0.5);

});


// ******************** EVENT HANDLING ********************

$(document).ready(function () {

    $(".thumbNav a").click(function (event) {
        event.preventDefault();
        $('div.thumbs li').css('display', 'none');                      // hide thumbs
        $('div.thumbs li.p' + $(this).html() + ' img').each(function (index) { //load thumbs
            if ($(this).attr('src') == "../images/misc/shim.gif") {
                $(this).attr('src', $(this).attr('title'));
                $(this).attr('title', '')
            } else {
                return false;    //break if src already set (for first; thus for all)
            }
        });

        $('div.thumbs li.p' + $(this).html()).css('display', 'block');  // display current page of thumbs
        $('.thumbNav a.active').removeClass('active');
        $('.thumbNav a.p' + $(this).html()).addClass('active');
    });

    //    RUNNING TOO SLOW: TBD

    //    $(".thumbs a").click(function(event) {
    //        event.preventDefault();
    //        var _pageName = $(this).attr('class');

    //        $('.thumbs li.active').fadeTo(0, 1);
    //        $('.thumbs li.active').removeClass('active');
    //        $('.thumbs li a.' + _pageName).parent('li').addClass('active');
    //        $('.thumbs li.active').fadeTo(0, 0.5);
    //        //alert(_pageName);
    //        window.location.hash = _pageName;

    //        fnAjaxRefresh(_pageName);

    //    });

});

// add thumb click

//re display url etc.


// ******************** FUNCTIONS ********************

function fnAjaxRefresh(_myPageName) {



    $('#setImageSrc').attr("src", "../images/photo_240px/" + _myPageName + ".jpg");
    

    fnFlickrUrl(_myPageName);
    fnBrowseNextURL(_myPageName);
    fnBrowsePreviousURL(_myPageName);
    fnBrowsePageNumStr(_myPageName);
    fnPhotoStoryStr(_myPageName);
    fnPhotoTitleStr(_myPageName);
    fnPhotoDescriptionStr(_myPageName);
    fnPhotoLocationStr(_myPageName);
    fnPhotoDateStr(_myPageName);
    fnPhotoSeriesStr(_myPageName);
    fnPhotoBuySelectHtml(_myPageName);
    fnPhotoCommentsHtml(_myPageName);

    $('#setUrlLarge').attr("href", "large.aspx?id=" + _myPageName); //link text issue doesn't occure when link is just file
    $('#setUrlImgLarge').attr("href", "large.aspx?id=" + _myPageName);     //link is to image
    $('#setUrlService').attr("href", "service.aspx?id=" + _myPageName);  //link text issue doesn't occure when link is just file
    $('#setUrlService2').attr("href", "service.aspx?id=" + _myPageName);
    
};


function fnFlickrUrl(_myPageName) {
    $("#setUrlFlickr").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/FlickrUrl",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            var _linkText = $("#setUrlFlickr").html();
            $("#setUrlFlickr").attr("href", msg.d);
            $("#setUrlFlickr").html(_linkText);      // JQUERY or IE BUG: text is replaced with url :((
            $("#setUrlFlickr").fadeTo(0, 1);
        }
    });
};

function fnBrowseNextURL(_myPageName) {
    $("#setUrlBrowseNext").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/BrowseNextURL",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            $("#setUrlBrowseNext").attr("href", msg.d);
            $("#setUrlBrowseNext").fadeTo(0, 1);
        }
    });
};

function fnBrowsePreviousURL(_myPageName) {
    $("#setUrlBrowsePrevious").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/BrowsePreviousURL",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            $("#setUrlBrowsePrevious").attr("href", msg.d);
            $("#setUrlBrowsePrevious").fadeTo(0, 1);
        }
    });
};

function fnBrowsePageNumStr(_myPageName) {
    $("#insertPageNum").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/BrowsePageNumStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            $('#insertPageNum').html(msg.d);
            $("#insertPageNum").fadeTo(0, 1);
                
        }
    });
};

function fnPhotoStoryStr(_myPageName) {
    $("#insertStory").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoStoryStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertStory').html(msg.d);
            $("#insertStory").fadeTo(0, 1);
            
            //jquery append somewhere
        }
    });
};

function fnPhotoTitleStr(_myPageName) {
    $("#insertTitle").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoTitleStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertTitle').html(msg.d);
            $("#insertTitle").fadeTo(0, 1);
        }
    });
};

function fnPhotoDescriptionStr(_myPageName) {
    $("#insertDescription").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoDescriptionStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertDescription').html(msg.d);
            $("#insertDescription").fadeTo(0, 1);
        }
    });
};

function fnPhotoLocationStr(_myPageName) {
    $("#insertLocation").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoLocationStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertLocation').html(msg.d);
            $("#insertLocation").fadeTo(0, 1);
            //jquery append somewhere
        }
    });
};

function fnPhotoDateStr(_myPageName) {
    $("#insertDate").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoDateStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertDate').html(msg.d);
            $("#insertDate").fadeTo(0, 1);
            //jquery append somewhere
        }
    });
};

function fnPhotoSeriesStr(_myPageName) {
    $("#insertSeries").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoSeriesStr",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertSeries').html(msg.d);
            $("#insertSeries").fadeTo(0, 1);
            //jquery append somewhere
        }
    });
};

function fnPhotoBuySelectHtml(_myPageName) {
    $("#insertBuySelect").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoBuySelectHtml",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert('buy select: ' + msg.d);
            $('#insertBuySelect').html(msg.d);
            var _MyForm = document.getElementById('mybuyform');
            var _intSelected = _MyForm.buyselect.options[_MyForm.buyselect.selectedIndex].value;
            myButton(_intSelected);
            $("#insertBuySelect").fadeTo(0, 1);
        }
    });
};

function fnPhotoCommentsHtml(_myPageName) {
    $("#insertComments").stop();            //stop waithing for last comment? TBD if this works in this context
    $("#insertComments").fadeTo(0, 0.2);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "wsDefault.asmx/PhotoCommentsHtml",
        data: "{'myPageName':'" + _myPageName + "'}",
        dataType: "json",
        success: function(msg) {
            //alert(msg.d);
            $('#insertComments').html(msg.d);
            $("#insertComments").fadeTo(0, 1);
        }
    });
};    
