$(document).ready(function() {
        //variables used here defined inline in HTML for dynamic values
        top.exd_space.refresher.ads2Refresh(new Array(galleryDiv, new Array(galleryMN,galleryW,galleryH, adPosition, 'I', galleryRefRate) ));

        var thumbCount = $('.thumbstrip-inner .thumb').length;
        var galleryTitle = "";
        var imageTitle = "";
        var baseGalleryUrl = 'http://'+document.location.host;
        var baseDir = '/photos/';
        if (location.href.indexOf('/gallery/') > -1) {
             baseDir = '/gallery/';
        }
	var permaTest = location.href.split(baseDir);
	var maxwidth = 0;
	var maxheight = 0;
	var picIndex = 0;
	setGallerySize();
	var galleryCount = 0;
	resizeImage($("#photoBody img"));

	$.historyInit(updateMainPicByHistory,location.href);
	if(permaTest.length == 2)
        {
		var part = permaTest[1].split('/#');
		var slug = jQuery.trim(part[0]);
				setPageOmniture(slug);
		var mediaID = jQuery.trim(part[1]);
		if (mediaID!='') {
	            loadGallery(mediaID);		   
		} else {
                   // var picID = getPicIDFromThumb($('.thumbstrip-inner .thumb:first').attr('id'));
                    // $.historyLoad(picID);        
		}

	}
	
	
	$('.gallery_open').click(function(){
		var slug = $(this).attr('rel');
		return false;
	});
	$('.thumbstrip-inner .thumb ').live('click',function(e){
             
             if (e.button == "0") {

			var picID = getPicIDFromThumb($(this).attr('id'));
		 	picIndex = $('.thumbstrip-inner .thumb').index(this);
			//updateMainPic(picID);
			$.historyLoad(picID);        
			return false;
              }
	});
	$('li#previous-image-top,li#previous-image-bottom').live('click',function(e){
                      if (e.button == "0") {
	        moveBack(); 
	        return false; 
	    }
	});
	
	$('li#next-image-top, #photoBody, li#next-image-bottom').live('click',function(e){ 
                      if (e.button == "0") {
	        moveUp(); 
	        return false; 
	    }
	});

    function setPageOmniture(slug) {
        if (typeof(s_265) != 'undefined' && typeof(s_265.prop1) != 'undefined') {
            s_265.pageName += " : " + document.title;
           
            // alert(s_265.pageName);
        } else if (typeof(s_prop1) != 'undefined') {
            // G Code 
            s_pageName += " : " + document.title;
        } else {
            // OMNITURE VALUES ARE NOT SET IN THE PAGE
        }
    }

    function moveBack()
	{

	// var oldPicIndex = $('#hidden-count').text();
	// oldPicIndex = parseFloat(oldPicIndex);
	var oldPicIndex = picIndex;
	var picCount = $('.thumbstrip-inner .thumb').length;
	if(oldPicIndex==0) picIndex = picCount-1;
	else  picIndex = oldPicIndex-1;
	// var galSlug = gallerySlug;
	var picID = getPicIDFromThumb($('.thumbstrip-inner .thumb').eq(picIndex).attr('id'));	
	// updateMainPic(picID);
	        $.historyLoad(picID);        
	}
	
    function moveUp()
	{

	// var oldPicIndex = $('#hidden-count').text();
	// oldPicIndex = parseFloat(oldPicIndex);
	var oldPicIndex = picIndex;
	var picCount = $('.thumbstrip-inner .thumb').length;
	if(oldPicIndex==(picCount-1)) picIndex = 0;
	else  picIndex = oldPicIndex+1;
	// var galSlug = gallerySlug;
	var picID = getPicIDFromThumb($('.thumbstrip-inner .thumb').eq(picIndex).attr('id'));
	// updateMainPic(picID);
		        $.historyLoad(picID);        
	}

function updateMainPic(picID)
	{
	if(location.href.match(/\/photos\//)) window.location.hash = picID;

        var pageUrl = baseGalleryUrl+baseDir+gallerySlug+'/';
        var resUrl = pageUrl;
        if (gallerySize != 'full') {
            resUrl = resUrl + 'full/#'+picID;
        } else {
            resUrl = resUrl + '#' + picID;
        }
	$.getJSON('/media-ajax/'+gallerySlug+'/'+picID,
            function(data){
                 $('#photoBody img').attr('alt',data.Gallery.Media[0].MediaTitle);
                 $('#photoBody img').attr('title',data.Gallery.Media[0].MediaTitle);
                 $('#photoBody img').attr('src',data.Gallery.Media[0].MediaSrc);
                 resizeImage($('#photoBody img'));
                 $('#mediaDescription').html(data.Gallery.Media[0].MediaDescription+
                                       '<span id="mediaCredit" class="credits">'+data.Gallery.Media[0].MediaCredit+
                                       '</span><span id="mediaDate" class="date">'+data.Gallery.Media[0].MediaDate+'</span>');
                 $('#mediaTitle').html(data.Gallery.Media[0].MediaTitle+' <span class="count">('+data.Gallery.Media[0].CurrentImageNum+' of '+data.Gallery.Media[0].TotalMedia+')</span>');
                 var oldPic = $('#hidden-count').text();
		 $('.thumb').eq(oldPic).removeClass('selected');
		 $('#hidden-count').text(picIndex);
		 $('.thumb').eq(picIndex).addClass('selected');

                 $('#resUrl').attr('href',resUrl);
		// $('#image-permalink').attr('href','http://www.blogsmithmedia.com/"+document.location.host+baseDir+'#'+galSlug+'+'+picID);
             });


	//top.exd_space.refresher.refresh_ads_string('gallery_ads');
	galleyAdRefresh();
	}
	
function loadGallery(picID) {
        if(picID>0)
        {
	   picIndex = $('.thumbstrip-inner .thumb').index($('#thumb-'+picID));
        } else {
	   picID = getPicIDFromThumb($('.thumbstrip-inner .thumb').eq(picIndex).attr('id'));
        }
        $.historyLoad(picID);        
}

function galleyAdRefresh() {
        galleryCount+=1;  
        if (galleryCount%galleryRefRate == 0) {
      	   scroll(0,0);           
           galleryCount = 0;
        }
        top.exd_space.refresher.refresh_ads_string(galleryDiv);
}

function getPicIDFromThumb(thumbID) {
    var picID = thumbID.split('-');
    return picID[1];
}

function resizeImage(image)
{

        if (image == undefined || image == null) return false;
           if (maxwidth == undefined) maxwidth = 615;
           if (maxheight == undefined) maxheight = 500;

        w = image.attr('width');
        h = image.attr('height');
        
        var newWidth = 0;
        var newHeight = 0;
        var ratio = 1;
        if( w == 0 || h == 0 ) {
 //           image.attr('height',maxheight);
            image.attr('width',maxwidth);
        } else if (w > h && w > maxwidth) {
                ratio = getNewSizeRatio(maxwidth, w);
                newHeight = ratio * h;
                image.attr('width',maxwidth);
//                image.attr('height',newHeight);
        } else if (w < h && h > maxheight) {
               ratio = getNewSizeRatio(maxheight,h);
               newWidth = ratio * w;
               image.attr('width',newWidth);
//               image.attr('height',maxheight);
        }
}

function getNewSizeRatio(val1, val2) {
    return  val1  / val2;
}
function setGallerySize() {
   if (gallerySize == 'small') {
      maxwidth = 615;
      maxheight = 500;
   } else if (gallerySize == 'full') {
      maxwidth = 1028;
      maxheight = 2000;
   }

}

function updateMainPicByHistory(picID) {
    picIndex = $('.thumbstrip-inner .thumb').index($('#thumb-'+picID));
    updateMainPic(picID);
}
        top.exd_space.refresher.mmx(galleryDiv, 'http://www.aolcdn.com/_media/channels/ke_blank.html', '');
		

});