// JScript File


var current_album;
function view_photo(index)
{
    view_photo_thumb(index)
}

function view_photo_thumb(index)
{
//    $("#photos-previewing").attr("src", ''); //Empty the preview photo
//    $('#pht_loading').show();
   
    photos_current_photoindex = index;
    p = Math.floor(photos_current_photoindex / 5)+1;
    
    if(p!=photos_current_page)
        preview_page(p);
    if(photos_current_photoindex==urlList.length-2)
    {
        $hide($$("photos-album-next-link"));
    }
    else
    {
        $show($$("photos-album-next-link"));
    }

    if(photos_current_photoindex==0)
    {
        $hide($$("photos-album-prev-link"));
    }
    else
    {
        $show($$("photos-album-prev-link"));    
    }
    var id=urlList[photos_current_photoindex][0];
    if(!$defined(id))return false;
    $("#photos-previewing").attr("src", urlList[photos_current_photoindex][1]);
    //$('#pht_loading').hide();
    $("#photo_id").val(id);
    
    $('#linkPhotoTitle').show();
    $('#linkPhotoTitle').next().hide();;
    
     $.post("/rpc/photos/photos.ashx",{id:id},
        function(xml) {
            photo = eval("(" + xml + ")");
            if($("#linkPhotoTitle")[0] != null) { //Owner
                if($.trim(photo.Title) == '')
                    $("#linkPhotoTitle").html('Please click here to update your photo title');
                else 
                    $("#linkPhotoTitle").html('<span class="photos-photo-title">' + photo.Title + '</span>');
                    
                if(photo.Des == '')
                    $('#linkPhotoDesc > span').html('Please click here to update your photo description');    
                else
                    $('#linkPhotoDesc > span').html(photo.Des);  
            }
            else {
                $("#spanPhotoTitle").html(photo.Title);
                $("#spanPhotoDesc").html(photo.Des);
            }

            tmp = '<span>' + photo.RateG + '</span> ';
            if(photo.Rated == 1)
              $('#tRateCount').html(tmp + '<img src="/images/rated.png" />');
            else 
              $('#tRateCount').html(tmp + '<a><img src="/images/good.png" alt="I like it!" onclick="photos_rate()" /></a>');
    });
    
    $('#linkPhotoDesc').show();
    $('#linkPhotoDesc').next().hide();
    $.post("/rpc/photos/comments.aspx",{
		photo_id: id
	      },function(xml){
            photos_current_photoid = id;
	 		$("#photo-comments").html(xml);
            bind_delete_events("#photo-comments div.blogbox");
	});    
	
}

function view_prev_photo()
{
    view_photo_thumb(photos_current_photoindex-1);
}
function view_next_photo()
{
    view_photo_thumb(photos_current_photoindex+1);
}

function photos_deletecomment(comment_id)
{
    $.post("/controllers/photoCtrl.ashx",{
        action:"delete_comment",
        comment_id:comment_id,
        photo_id:$("#photo_id").val()
          },function(xml) {
            ProcessRPCResult(xml)
            $("#pht_comment_" + comment_id).fadeOut(1000);
    });
}

function photos_deletecurrent(album_id, photo_id)
{
    window.location= "/photos/confirm.aspx?confirm=DeletePhoto&album_id=" +album_id + "&photo_id=" + photo_id;
}

function photos_editphoto(album_id, photo_id)
{
    window.location= "/photos/edit.aspx?album_id=" +album_id + "&photo_id=" + photo_id;
}

function photos_deletealbum(album_id)
{
    window.location= "/photos/confirm.aspx?confirm=DeleteAlbum&album_id=" +album_id;
}

var photos_current_preview_style="thumb";
function preview_album(id)
{
    current_album = id;
    $("#photo_id").val(id);
    m = "";
    if (photos_current_preview_style=="detail")
    {
        m = "detail";
    }
    $show($$("photos-preview-div"));
    $(".photo-album-list-content-icon-selected").removeClass("photo-album-list-content-icon-selected").addClass("photo-album-list-content-icon");
    $("#album" + id).addClass("photo-album-list-content-icon-selected");
    ajah("/rpc/photos/album_preview.aspx","photo-preview-list",{album_id:id,method:m});
}

function switch_preview_style(tab)
{
    if(tab==photos_current_preview_style)return false;
    photos_current_preview_style=tab;
    if(tab=="detail")
    {
        $("#photo-preview-detail-li").addClass("active");
        $("#photo-preview-thumb-li").removeClass("active");
    }
    else
    {
        $("#photo-preview-detail-li").removeClass("active");
        $("#photo-preview-thumb-li").addClass("active");
    }
    preview_album(current_album);
}

var photos_current_tab=null;
function photos_showTab(tab)
{
    if(photos_current_tab!=null)
    {
        $("#" + photos_current_tab + "Div").hide();
        $("#" + photos_current_tab + "Tab").removeClass("active");
    }
    d = tab.id.substring(0, tab.id.length -3);

    if (photos_current_tab!=d)
    {
        photos_current_tab =d;
        $("#" + photos_current_tab + "Div").slideDown();
        $("#" + photos_current_tab + "Tab").addClass("active");
    }
    else
        photos_current_tab =null;
}

function preview_page(page)
{   
    photos_current_page=page;
    ajah('/rpc/photos/album_photo_list.aspx','photos_list_albumphoto', {album_id:photos_current_album_id, page:photos_current_page});
}

function photos_rate()
{
    rating('/rpc/photos/RatingProxy.ashx', urlList[photos_current_photoindex][0], 'photo' , 1, $('#tRateCount').find('span'));
}
$(document).ready(function() {
    $('#imgRateG').click(function() {
        photos_rate();
    });
    
    //Photo Description
    $('#linkPhotoDesc').click(function() {
        $(this).hide();
        $(this).next().show();
        var desc = $(this).children('span').html().replace(/<br>/g,'\n');
        if(desc != 'Please click here to update your photo description') {
            if($.browser.msie) {
                $('#ulPhotoDescUpdate > li:eq(0) textarea').html(desc);
            }else{
                $('#ulPhotoDescUpdate > li:eq(0) textarea').val(desc);
            }
        }
        else 
            $('#ulPhotoDescUpdate > li:eq(0) textarea').val('');
            
    });
    
    var count = 0;
    $('#ulPhotoDescUpdate > li').each(function(){
        if(count === 1) {
            this.onclick = function() {
                var desc;
                if($.browser.msie) {
                    desc = $('#txtPhotoDesc')[0].innerHTML.replace(/\n/g,"<br/>");
                }else{
                    desc = $('#txtPhotoDesc').val().replace(/\n/g,"<br/>");
                }
                $('#linkPhotoDesc > span').html(desc);
                updateDesc(desc); // Update Description
                               
                $('#linkPhotoDesc').show();
                $('#linkPhotoDesc').next().hide();
            };
        }else if(count === 2){
            this.onclick = function() {
              $('#linkPhotoDesc').show();
              $('#linkPhotoDesc').next().hide();
            };
        }
        count++;
    });
    
    //Photo Title
    $('#linkPhotoTitle').click(function() {
        $(this).hide();
        $(this).next().show();
        var title = $(this).children('span').html();
        if($.trim(title) != 'Please click here to update your photo title') {
            $('#txtPhotoTitle').val(title);
        }
        else 
            $('#txtPhotoTitle').val('');
    });
   
    count = 0;
    $('#ulPhotoTitleUpdate > li').each(function(){
        if(count === 0) {
            this.onclick = function() {
                var title = $('#txtPhotoTitle').val();
                $('#linkPhotoTitle').html(title);
                updateTitle(title); //Update Title
                
                $('#linkPhotoTitle').show();
                $('#linkPhotoTitle').next().hide();
            };
        }else {
            this.onclick = function() {
              $('#linkPhotoTitle').show();
              $('#linkPhotoTitle').next().hide();
            };
        }
        count++;
    });
    var posting=false;
    $('#frn_btnPostComment').click(function() {
        if(posting)return false;
        if($('#txtComment').val().length==0)
        {
            alert("Please input comment content.");
            return false;
        }

        posting=true;
        tmp = $(this).html();
        if(_userID==0) window.location="/login.aspx?LoginRequired=True&ReturnUrl=" + $currenturl();
        $(this).html('<img src="/images/ajaxing.gif" />');
        data = getformArray("comments");
        $.post("/controllers/photoCtrl.ashx",
            data
        , function(xml) {
            posting=false;
            ProcessRPCResult(xml);
            if(xml.length > 6 && xml.substr(0, 5) == "alert")
            {
                alert(xml.substr(5));
            }
            $('#frn_btnPostComment').html(tmp);
            $('#txtComment').val('');
            $.post("/rpc/photos/comments.aspx",{
                photo_id:urlList[photos_current_photoindex][0]
                  },function(xml) {
                    $("#photo-comments").html(xml);
                    bind_delete_events("#photo-comments div.blogbox");
            });
        }); 
    });
    
    //Album Title
    $('#linkEditAlbumTitle').click(function() {
        $(this).hide();
        $(this).next().show();
        var title = $(this).children('span').html();
        if($.trim(title) != 'Please click here to update your album title') {
            $('#txtAlbumTitle').val(title);
        }
        else 
            $('#txtAlbumTitle').val('');
    });
    
    count = 0;
    $('#ulAlbumTitleUpdate > li').each(function(){
        if(count === 0) {
            this.onclick = function() {
                var title = $('#txtAlbumTitle').val();
                $('#linkEditAlbumTitle').children('span').html(title);
                updateAlbumTitle(title); //Update Title
                
                $('#linkEditAlbumTitle').show();
                $('#linkEditAlbumTitle').next().hide();
            };
        }else {
            this.onclick = function() {
              $('#linkEditAlbumTitle').show();
              $('#linkEditAlbumTitle').next().hide();
            };
        }
        count++;
    });
    
    //Album Description
    $('#linkEditAlbumDesc').click(function() {
        $(this).hide();
        $(this).next().show();
        var desc = $(this).children('span').html().replace(/<br>/g,'\n');
        if(desc != 'Please click here to update your album description') {
            if($.browser.msie) {
                $('#ulAlbumDescUpdate > li:eq(0) textarea').html(desc);
            }else{
                $('#ulAlbumDescUpdate > li:eq(0) textarea').val(desc);
            }
        }
        else 
            $('#ulAlbumDescUpdate > li:eq(0) textarea').val('');
            
    });
    
    count = 0;
    $('#ulAlbumDescUpdate > li').each(function(){
        if(count === 1) {
            this.onclick = function() {
                var desc;
                if($.browser.msie) {
                    desc = $('#txtAlbumDesc')[0].innerHTML.replace(/\n/g,"<br/>");
                }else{
                    desc = $('#txtAlbumDesc').val().replace(/\n/g,"<br/>");
                }
                $('#linkEditAlbumDesc > span').html(desc);
                updateAlbumDesc(desc); // Update Description
                               
                $('#linkEditAlbumDesc').show();
                $('#linkEditAlbumDesc').next().hide();
            };
        }else if(count === 2){
            this.onclick = function() {
              $('#linkEditAlbumDesc').show();
              $('#linkEditAlbumDesc').next().hide();
            };
        }
        count++;
    });
});

function updateTitle(title) {
    $.post("/controllers/photoCtrl.ashx",{
		action: 'update_title', 
		  photoId: urlList[photos_current_photoindex][0], 
		    title: $('#txtPhotoTitle').val()
	      }, function(xml) {}); 
}

function updateDesc(description) {
    $.post("/controllers/photoCtrl.ashx",{
        action: 'update_desc', 
		 photoId: urlList[photos_current_photoindex][0], 
		    desc: description
	      }, function(xml) {}); 
}

function updateAlbumTitle(newTitle) {
    $.post("/controllers/photoCtrl.ashx",{
		action: 'update_album_title', 
		  albumId: photos_current_album_id, 
		    title: newTitle
	      }, function(xml) {
	        window.location.reload(true);//Temp Solution
	      }); 
}


function updateAlbumDesc(description) {
    $.post("/controllers/photoCtrl.ashx",{
		action: 'update_album_desc', 
		  albumId: photos_current_album_id, 
		    desc: description
	      }, function(xml) {}); 
}

var comments = function(){
    return {
        reply:function(related_id){
            $("#related_id").val(related_id);
            var nick;
            nick = $("#pht_comment_" + related_id).find(".author a").text();            
            $("#commentsform").focus();
            $("#txtComment").focus();
            $("#txtComment").val("إلى " + nick + ", ");
        }
    }
}();
