jQuery.noConflict();

// we need those variables in the global scope, out of the local scope
var slideshowpos
var total_width = 0;
var new_height = 250;
var new_width = 0;

jQuery('document').ready( function() {

jQuery(window).load(function() {

if (jQuery('#curtain').length > 0) {
	
	
	// breadcrumbs
	if (jQuery('.current_page_item').length > 0) {
		var current_title = jQuery('.current_page_item').children('a').text();
		jQuery('.current_page_item').children('a').html('&middot;'+' '+current_title);
	}
	
	if (jQuery('.current_page_parent').length > 0) {
		var current_page_parent_title = jQuery('.current_page_parent').children('a').text();
		jQuery('.current_page_parent').children('a').html('&middot;'+' '+current_page_parent_title);
	}
	
	//DETAILS PAGE - NO SCROLL BUTTONS, OVERFLOW AUTO 
	if (jQuery('ol#posts').length == 0) {
		jQuery('div#buttons').hide();
		jQuery('html').css({'overflow-y':'auto'});
	}



	/* resize images proportionately	*/
	/* and calculate total width		*/
	
	//var i = 0;
	jQuery('.slideshow img').each(function(){
	    //jQuery(this).load(function(){
		    //i++;
		    //console.log(i);
		    //new_width = (new_height / jQuery(this).attr('height') * jQuery(this).attr('width'));
		    new_width = (new_height / jQuery(this).height() * jQuery(this).width());
		    //console.log(i, ' ', new_width);
		    jQuery(this).css({
			    'width': new_width,
			    'height': new_height
		    });
		    jQuery(this).css({
			    'border': 'none',
			    'margin-right': '4px'
		    });
		    total_width = total_width + new_width + 4;
		    //total_width = total_width + jQuery(this).width() + 4;
	        //console.log(i, ' ', total_width);
        //});
	});
    //jQuery('.slideshow img:last').load( function(){
	    total_width = total_width - 4;
	    //jQuery('ol#posts').css({'width':total_width});
	    jQuery('ol#posts').css({'width':total_width});
	//});
	jQuery('div.post * img:last').css({'margin-right': '0px'});
	

// position slideshow vertically central
	
	slideshowpos = ((jQuery(window).height()/2)-(new_height/2));
	if (slideshowpos < 50) {
		slideshowpos = 50;
		jQuery('.slideshow').css({'top':slideshowpos});
	} else {
		jQuery('.slideshow').css({'top':slideshowpos});
	}	
	
// and buttons underneath
	jQuery('#buttons').css({'top':(slideshowpos+new_height+15)});
	
//push height to containing div(s)
	if (jQuery('.detailstext').length < 1) {
		jQuery('.slideshow').css({'height':new_height+30+'px'});
	}
	
	if (jQuery('div.slideshow').scrollLeft() == 0) {
		jQuery('div#buttons #previous').css({'opacity':'0.25'});
	}
	
	var scrollnextpos = parseInt(jQuery('ol#posts').css('width')) - parseInt(jQuery('div.slideshow').scrollLeft());

	if (scrollnextpos <= parseInt(jQuery('div.slideshow').css('width'))) {
		jQuery('div#buttons #next').css({'opacity':'0.25'});
	}

 // CENTRE DETAILS
if (jQuery('.detailstext').length > 0) {
var slideshowtop = ((jQuery(window).height()/2)-(jQuery('.detailstext').height()));
	if (slideshowtop < 50) {
		slideshowtop = 50;
	}
jQuery('.slideshow').css({'top':slideshowtop});
}

jQuery('span#loading_gif').hide();
jQuery('#curtain').css({'display':'none'});
 }

}); // closing the .load function

jQuery('html').css({'overflow-y':'auto'});

//hover photo fade - maybe animate this?
	jQuery('div.post * img').hover(
	function(){jQuery(this).css({'opacity': '0.5'})},
	function(){jQuery(this).css({'opacity': '1'})}
	);

	//scroll perfect	
		var destination = jQuery('div.post * img:first');
		var hovering_next = 0;
		var hovering_previous = 0;
		var destinationfirstimg = jQuery('ol#posts * img:first').width() + 10;
		var scrollpreviouspos;
		var scrollnextpos;
		
		function scrollpreviouscheck() {
			if ((destination).parent().parent().parent('li.postWrapper').prev().prev().prev().children().children().children('img').length > 0) {
				destination = (destination).parent().parent().parent('li.postWrapper').prev().prev().prev().children().children().children('img');
				scrollprevious();
			} else if ((destination).parent().parent().parent('li.postWrapper').prev().prev().children().children().children('img').length > 0) {
				destination = (destination).parent().parent().parent('li.postWrapper').prev().prev().children().children().children('img');
				scrollprevious();
			} else if ((destination).parent().parent().parent('li.postWrapper').prev().children().children().children('img').length > 0) {
				destination = (destination).parent().parent().parent('li.postWrapper').prev().children().children().children('img');
				scrollprevious();
			} else {
				destination = destination;
			}
		}
		
		function scrollnextcheck() {	
			if ((destination).parent().parent().parent('li.postWrapper').next().next().next().children().children().children('img').length > 0) {
				destination = (destination).parent().parent().parent('li.postWrapper').next().next().next().children().children().children('img');
				scrollnext();
			} else if ((destination).parent().parent().parent('li.postWrapper').next().next().children().children().children('img').length > 0) {
				destination = (destination).parent().parent().parent('li.postWrapper').next().next().children().children().children('img');
				scrollnext();
			} else if ((destination).parent().parent().parent('li.postWrapper').next().children().children().children('img').length > 0) {
				destination = (destination).parent().parent().parent('li.postWrapper').next().children().children().children('img');
				scrollnext();
			} else {
				destination = destination;
			}
		}
		
		function scrollprevious() {						
							jQuery('.slideshow').scrollTo(destination, 350, {easing:'linear'});
							scrollpreviouspos = parseInt(jQuery('ol#posts').css('width')) - parseInt(jQuery('div.slideshow').scrollLeft());
								if (scrollpreviouspos <= parseInt(jQuery('div.slideshow').css('width'))) {
									jQuery('div#buttons #next').css({'opacity':'0.25'});
									jQuery('div#buttons #previous').css({'opacity':'1'});
								} else if (parseInt(jQuery('div.slideshow').scrollLeft()) <= destinationfirstimg) { 
									jQuery('div#buttons #previous').css({'opacity':'0.25'});
									jQuery('div#buttons #next').css({'opacity':'1'});								
								} else {
									jQuery('div#buttons #previous').css({'opacity':'1'});
									jQuery('div#buttons #next').css({'opacity':'1'});
								}
		}

		function scrollnext() {			
						jQuery('.slideshow').scrollTo(destination, 350, {easing:'linear'});
						scrollnextpos = parseInt(jQuery('ol#posts').css('width')) - parseInt(jQuery('div.slideshow').scrollLeft());
								if (scrollnextpos <= parseInt(jQuery('div.slideshow').css('width'))) {
									jQuery('div#buttons #next').css({'opacity':'0.25'});
									jQuery('div#buttons #previous').css({'opacity':'1'});
								} else {
									jQuery('div#buttons #previous').css({'opacity':'1'});
									jQuery('div#buttons #next').css({'opacity':'1'});
								}
		}
		
//MOUSE WHEEL SCROLL

				jQuery('.slideshow').mousewheel(function(event, delta) {
						if (delta > 0) {

						if ((destination).parent().parent().parent('li.postWrapper').prev().children().children().children('img').length > 0) {
							destination = (destination).parent().parent().parent('li.postWrapper').prev().children().children().children('img');
							jQuery('.slideshow').scrollTo(destination, 50, {easing:'linear'});
							scrollpreviouspos = parseInt(jQuery('ol#posts').css('width')) - parseInt(jQuery('div.slideshow').scrollLeft());
								if (scrollpreviouspos <= parseInt(jQuery('div.slideshow').css('width'))) {
									jQuery('div#buttons #next').css({'opacity':'0.25'});
									jQuery('div#buttons #previous').css({'opacity':'1'});
								} else if (destination.attr('src') == jQuery('ol#posts * img:first').attr('src')) { 
									jQuery('div#buttons #previous').css({'opacity':'0.25'});
									jQuery('div#buttons #next').css({'opacity':'1'});
								} else {
									jQuery('div#buttons #previous').css({'opacity':'1'});
									jQuery('div#buttons #next').css({'opacity':'1'});
								}
						} else {
							destination = destination;
						}

						} else if (delta < 0) {
						
						if ((destination).parent().parent().parent('li.postWrapper').next().children().children().children('img').length > 0) {
							destination = (destination).parent().parent().parent('li.postWrapper').next().children().children().children('img');
						jQuery('.slideshow').scrollTo(destination, 50, {easing:'linear'});
							scrollnextpos = parseInt(jQuery('ol#posts').css('width')) - parseInt(jQuery('div.slideshow').scrollLeft());
								if (destination.attr('src') == jQuery('ol#posts * img:last').attr('src')) {
									jQuery('div#buttons #next').css({'opacity':'0.25'});
									jQuery('div#buttons #previous').css({'opacity':'1'});
								} else {
									jQuery('div#buttons #previous').css({'opacity':'1'});
									jQuery('div#buttons #next').css({'opacity':'1'});
								}
						} else {
							destination = destination;
						}
	  		
						}

						event.stopPropagation();
						event.preventDefault();
					});


        //////////////////////////////////////////////////
        //CLICKING NEXT AND PREVIOUS OUTSIDE OF SHADOWBOX
        /////////////////////////////////////////////////
		jQuery('#buttons a#previous').mousedown(function(){
			scrollpreviouscheck();
		});

		jQuery('#buttons a#next').mousedown(function(){
			scrollnextcheck();
		});
		
		/*
		// KEYBOARD FUN
        jQuery(document).keydown(function(e) {
	        switch(e.keyCode) { 
            	// User pressed left arrow
                case 37:
			        scrollpreviouscheck();
                break;
                // User pressed right arrow
                case 39:
                    scrollnextcheck();
                break;
	        }
	        return false;
        });
		*/

	jQuery('a#shadowbox_nav_moreinfo').show();
	
	var img_in_question;
	
	var poppedup = 0;
	
function dealwithcaption() {
			jQuery('div.popup_main').hide();	
			if ((img_in_question).parent().next('.caption').text().replace(/\s/g,'').length > 0 || imgclass == "img" && (img_in_question).parent().attr('title').length > 0) {
			//console.log('looks good to me');
				var total_button_width = 0;
				jQuery('a#shadowbox_nav_moreinfo').css({'width':'3em', 'margin-top':'0.15em'});				
				jQuery('a#shadowbox_nav_moreinfo').show();		
				if (imgclass == "mov") {
					jQuery('div.popup_main').html((img_in_question).parent().next('.caption').html()+"<a id=\"closeinfo\" title=\"Close Info\">"+jQuery('span#closeinfo_standby').html()+"</a>");
				} else {
					jQuery('div.popup_main').html("<h1>"+(img_in_question).parent().attr('title')+"</h1>\n"+(img_in_question).parent().next('.caption').html()+"<a id=\"closeinfo\" title=\"Close Info\">"+jQuery('span#closeinfo_standby').html()+"</a>");
				}
				moreinfobutton();						
			} else {
				jQuery('a#shadowbox_nav_moreinfo').hide();
				//console.log('this isn\'t right');
			}
			return false;
		}

function moreinfobutton() {
		jQuery('a#shadowbox_nav_moreinfo').click( function() {
			if (imgclass == "mov") {
				if (poppedup == 1) {
    				jQuery('div.popup_main').hide();
    				poppedup = 0;
    				jQuery('#shadowbox_body_inner').show();
    			} else if (poppedup == 0) {
    				jQuery('div.popup_main').css({'top':jQuery('#shadowbox').position().top+50, 'left':jQuery('#shadowbox').position().left+50, 'width':(jQuery('#shadowbox').width()-110)});
    				jQuery('#shadowbox_body_inner').hide();
					jQuery('div.popup_main').show();
					poppedup = 1;			
				}
			} else {
    			if (poppedup == 1) {
    				jQuery('div.popup_main').hide();
    				poppedup = 0;
    			} else if (poppedup == 0) {

    			// shadowbox is displayed differently if in safari
    			// if in safari, it will use margin to position the eleemnt - in all other browser it shoudl use the position
    			if (jQuery.browser.safari) {
    				left_position_caption = parseInt(jQuery('#shadowbox').css('margin-left'));
    			} else {
    				left_position_caption = jQuery('#shadowbox').position().left
    			}
    			 			
 	   				jQuery('div.popup_main').css({'top':jQuery('#shadowbox').position().top+50, 'left':left_position_caption+50, 'width':(jQuery('#shadowbox').width()-110)});
					jQuery('div.popup_main').show();
					poppedup = 1;			
    			} else {
    			} 
    		}
	    });
	    return false;
	}

var imgclass;

//pop-up caption pops up, buttons disappear
jQuery('div.post * img').click(function() {				
		img_in_question = jQuery(this);
		imgclass = (img_in_question).attr("class");
		if (imgclass == "img") {
			jQuery('#shadowbox_info, #spacer').hide();//#shadowbox_title, 
			/* !where we hide the title of the shadowbox */
			jQuery('#shadowbox_title_inner').hide();
		}
		dealwithcaption();
		jQuery('#shadowbox_nav').css({'top':(slideshowpos+new_height+15)});
		jQuery('div#buttons').css({'display':'none'});		
	    		
	//navigate previous	and main 'slider' scrolls
		jQuery('a#shadowbox_nav_previous').click( function() { 
			jQuery('#shadowbox_body_inner').show();
				if ((img_in_question).parent().parent().parent('li.postWrapper').prev().children().children().children('img').length > 0) {
					img_in_question = (img_in_question).parent().parent().parent('li.postWrapper').prev().children().children().children('img');
					imgclass = (img_in_question).attr("class");
					if (imgclass == "img") {
						jQuery('#shadowbox_title_inner, #shadowbox_info, #spacer').hide();
					}
					poppedup = 0;		
					jQuery('div.popup_main').hide();
					jQuery('a#shadowbox_nav_moreinfo').unbind('click');
					dealwithcaption();
					shadowboxmodescrollprevious();
				}
				return false;
    	});
    	   	    	   	
	//navigate next	and main 'slider' scrolls
		jQuery('a#shadowbox_nav_next').click( function() { 
			jQuery('#shadowbox_body_inner').show();
				if ((img_in_question).parent().parent().parent('li.postWrapper').next().children().children().children('img').length > 0) {
					img_in_question = (img_in_question).parent().parent().parent('li.postWrapper').next().children().children().children('img');
					imgclass = (img_in_question).attr("class");
					if (imgclass == "img") {
						jQuery('#shadowbox_title_inner, #shadowbox_info, #spacer').hide();
					}
					poppedup = 0;
					jQuery('div.popup_main').hide();
					jQuery('a#shadowbox_nav_moreinfo').unbind('click');
					dealwithcaption();
					shadowboxmodescrollnext();
				}
				return false;
    	});   	

// KEYBOARD FUN
jQuery(document).keydown(function(e) {
	switch(e.keyCode) { 
    	// User pressed left arrow
        case 37:
			jQuery('a#shadowbox_nav_previous').click();
        break;
        // User pressed right arrow
        case 39:
            jQuery('a#shadowbox_nav_next').click();
        break;
	}
	return false;
});

// 'CLOSE INFO' BUTTON CLOSES INFO, DUH!
			jQuery('a#closeinfo img').livequery ('click', function() {
			if (jQuery('#shadowbox_body').children().children('img').length == 0) {
    				jQuery('div.popup_main').hide();
    				poppedup = 0;
    				jQuery('#shadowbox_body_inner').show();
			} else {
    				jQuery('div.popup_main').hide();
    				poppedup = 0;
    		}
	    	});

// CLOSE: HIDE CAPTION AND BUTTON, SHOW REGULAR BUTTONS	
		jQuery('a#shadowbox_nav_close, div#shadowbox_overlay').click( function() { 
			poppedup = 0;
			jQuery('#shadowbox_body_inner').show();
			jQuery('div.popup_main').hide();
			jQuery('a#shadowbox_nav_moreinfo').hide();
			jQuery('div#buttons').css({'display':'block'});
			jQuery('a#shadowbox_nav_moreinfo').unbind('click');
			jQuery('a#shadowbox_nav_close, div#shadowbox_overlay').unbind('click');
			jQuery('a#shadowbox_nav_previous').unbind('click');
			jQuery('a#shadowbox_nav_next').unbind('click');
			jQuery(document).unbind('keydown');
		    return false;
    	});
});



// SHADOWBOX MODE NEXT/PREV IMG SCROLLS MAIN DISPLAY
	function shadowboxmodescrollprevious() {
		if ((destination).parent().parent().parent('li.postWrapper').prev().children().children().children('img').length > 0) {
			destination = (destination).parent().parent().parent('li.postWrapper').prev().children().children().children('img');
			scrollprevious();
		} else {
			destination = destination;
		}
		return false;
	}	

	function shadowboxmodescrollnext() {
		if ((destination).parent().parent().parent('li.postWrapper').next().children().children().children('img').length > 0) {
			destination = (destination).parent().parent().parent('li.postWrapper').next().children().children().children('img');
			scrollnext();
		} else {
			destination = destination;
		}
		return false;
	}

// IN SHADOWBOX MODE
// CLICK IMG TO VIEW NEXT 
// OR CLICK MOV TO HIDE POP-UP CAPTIONS AND PLAY MOV
jQuery('#shadowbox_body').livequery('click', function() {
	if (jQuery(this).children().children('img').length > 0) {
		jQuery('a#shadowbox_nav_next').click();
	} else if (poppedup == 1) {
 		jQuery('div.popup_main').hide();
  		poppedup = 0;
    	jQuery('#shadowbox_body_inner').show();
	}
});	
	});
