
var currentImage;
var currentOffset = 0;

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName];
    } else {
        return document[movieName];
    }
}

$('document').ready(function () {
	// setup a container for preloading content safely
	$('body').append('<div id="preloader"></div>');
	$('#preloader').css('display','none');
	
	// preload the nav rollovers
	$(".navigation ul li img").each(function (i) {
		$('#preloader').append('<img src="'+$(this).attr('src').replace(/\.jpg/,'-over.jpg')+'" alt="" />');
	});
		
	// setup nav hover events
	$(".navigation ul li img").hover(function(){ navOver(this); }, function(){ navOut(this); });
	
	// fade in the image
	$("#photo img").fadeOut(1);
	
	$('#staticPagination').hide();
	var pageCount = $('#staticPagination li').length;
	// swfobject.embedSWF("myContent.swf", "myContent", "300", "120", "9.0.0","expressInstall.swf", flashvars, params, attributes);
	var flashvars = {pageCount:pageCount};
	var params = {wmode:'transparent'};
	swfobject.embedSWF("/swf/paginator.swf", "paginator", "308", "30", "9.0.0", null, flashvars, params);
    
    $('#nextButton').click(next);
    $('#previousButton').click(previous);
    
    // $('body').append('<iframe id="hiddenIframe" name="hiddenIframe" style="display:none"></iframe>');
 
	/* photo list sorting */
	if ($('#photolist').length) {
    	$('.ordering').css('display', 'none');
    	$('.ordering input').addClass('order_field');
    	$('#photolist').sortable({update:function () { setTimeout(updateForm, 500); }});
	}
    
    /* photo title embed */
    $('.photoTitle h2').hide();
    $('.photoTitle').append('<div id="flashTitle"></div>');
    
    var flashvars = {};
    var params = {wmode:'transparent'};
    swfobject.embedSWF("/swf/photoTitle.swf", "flashTitle", "100", "22", "9.0.0", null, flashvars, params);
    
    setTimeout(function () { changeFlashText($('.photoTitle h2').html()); }, 1000);
    
    setTimeout(function () {
        $('#staticPagination li a').each(function (i) {
            if ($(this).attr('id') == 'activePage') {
                thisMovie('paginator').updateActivePage(null, i, true);
                currentOffset = i;
                return;
            }
    })
    }, 1000);
    
    
});

function navOver (targ) {
	$(targ).attr('src', $(targ).attr('src').replace(/\.jpg/, '-over.jpg'));
}
function navOut (targ) {
	
	$(targ).attr('src', $(targ).attr('src').replace(/\-over\.jpg/, '.jpg'));
}


/* fade in enhancement */
window.onload = fadeFirstImage;

function fadeFirstImage(){

	if ($('#photo img').length) {
		//$('#photo').height($('#photo').height());
		$('#photo img').fadeIn('slow');
	}
}




function changePhoto (val) {
	
    thisMovie('paginator').updateActivePage(null, val, true);
    
    window.location.href = "#photo_"+(val+1);
    
    $('#photo img').load(fadeInPhoto); 
    
    $('#photo').height($('#photo img').height());
	$('#photo img').fadeOut('slow', function () {
										currentImage = $('#staticPagination li:eq('+val+') a').attr('href');	
										currentOffset = val;
                                        
                                        updatePageArrows();
                                        
                                        $.getJSON('/photos/imagepath/'+currentImage.replace(/.*\/([0-9]{1,})$/, '$1'), 
                                                function (data) {
                                                    currentImage = data.filepath;
                                                    document.title = data.title;
                                                    $('.photoTitle h2').html(data.title);
                                                    changeFlashText(data.title);
                                                    
                                                    $('#photo img').attr('src', currentImage);
                                                    //$('#hiddenIframe').attr('src', currentImage);
										            // frames['hiddenIframe'].location.href = currentImage;
                                                });
										
									});
}

function next () {
    if (currentOffset < $('#staticPagination li').length-1){
        changePhoto(++currentOffset);
    }
    return false;
}

function previous () {
    if (currentOffset > 0) {
        changePhoto(--currentOffset);
    }
    return false;
}

function updatePageArrows() {
    if (currentOffset == 0) {
        $('#previousButton').hide();
    } else {
        $('#previousButton').show();
    }
    
    if (currentOffset >= $('#staticPagination li').length-1) {
        $('#nextButton').hide();
    } else {
        $('#nextButton').show();
    }
}

function fadeInPhoto () {
    var func = function () {
         $('#photo img').fadeIn('slow');
    }
    
     $("#photo").animate({ height: $('#photo img').height()+"px" }, 500, null, func );
}

function makeImagePath(str) {
	return str.replace('photographs/', 'photography/')+".jpg";
}

function updateForm () {
    $('input.order_field').each(function (i) {
        $(this).val(i+1);
    });
}
 
function updatePhotoTitle(width, height) {

    $('#flashTitle').width(width).height(height+3);

}

function changeFlashText (str) {
    try {
        thisMovie('flashTitle').setText(str);
    } catch (e) {
        $('#flashTitle').remove();
        $('.photoTitle h2').show();
    }
}
 
