jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery("<img>").attr("src", arguments[i]);
  }
};

$(document).ready(function(){
    /* for all browsers except IE 6 */
/*
    if (!($.browser.msie && $.browser.version < 7)) {
    	if ($('body').hasClass('home')) {
    		window.resizeTo(1000, 860);
    	} else if (window.innerWidth != 1015) {
    		//	863 - no vertical scroll
    		if (window.outerHeight > 860 && !$('body').hasClass('portfolio'))
    			window.resizeTo(1015, 860);
    		else
    			window.resizeTo(1015, window.outerHeight);
    	}
    }
*/

	/* IE css hack */
	$('body').css({'min-height': '750px'});


	/* begin image preload */
	var allImgArray = [];

	$('body.portfolio img').each(function(){
		allImgArray = allImgArray.concat($(this).attr('src'));
	});


	if ($('body.home').length || $('body.about').length) {

		if ($('body.home').length) {
			allImgArray = allImgArray.concat('/images/filmstrips/portfolio.jpg');
		}
		if ($('body.about').length) {
			allImgArray = allImgArray.concat('/images/filmstrips/about.jpg');
		}
	}
	allImg = allImgArray.join(', ');
	$.preloadImages(allImg);

	/* end image preload */



	/* to make it valid XHTML, we use "title" instead of "rel" in HTML */
	$('body.portfolio div.portfolio-images li img').each(function(){
		$(this).attr({rel: $(this).attr('title'), title: ''});
	});

	$('body.portfolio div.portfolio-images li:not(.current) img').hover(function(){
		$(this).attr({src: $(this).attr('rel'), rel: $(this).attr('src')});
	}, function(){
		$(this).attr({src: $(this).attr('rel'), rel: $(this).attr('src')});
	});

	var currentImg = $('body.portfolio div.portfolio-images li.current img');
	currentImg.attr({src: currentImg.attr('rel'), rel: currentImg.attr('src')});



	function portfolioBefore(event, action) {
		if (action == 'over' || action == 'clicked') {
			$(event).removeClass('inactive');
			$(event).addClass('current');
			$(event).parent().find('p#p-text-after').addClass('inactive');
			$(event).parent().find('p#p-text-after').removeClass('current');

			$('p#p-img-before').removeClass('inactive');
			$('p#p-img-before').addClass('current');
			$('p#p-img-after').addClass('inactive');
			$('p#p-img-after').removeClass('current');

			if (action == 'clicked') {
				$(event).addClass('clicked');
				$(event).parent().find('p#p-text-after').removeClass('clicked');
			}
		} else if (action == 'out' && !$(event).hasClass('clicked')) {
			$(event).addClass('inactive');
			$(event).removeClass('current');
			$(event).parent().find('p#p-text-after').removeClass('inactive');
			$(event).parent().find('p#p-text-after').addClass('current');

			$('p#p-img-before').addClass('inactive');
			$('p#p-img-before').removeClass('current');
			$('p#p-img-after').removeClass('inactive');
			$('p#p-img-after').addClass('current');
		}
	}

	function portfolioAfter(event, action) {
		if (action == 'over' || action == 'clicked') {
			$(event).removeClass('inactive');
			$(event).addClass('current');

			$(event).parent().find('p#p-text-before').addClass('inactive');
			$(event).parent().find('p#p-text-before').removeClass('current');

			$('p#p-img-after').removeClass('inactive');
			$('p#p-img-after').addClass('current');
			$('p#p-img-before').addClass('inactive');
			$('p#p-img-before').removeClass('current');

			if (action == 'clicked') {
				$(event).addClass('clicked');
				$(event).parent().find('p#p-text-before').removeClass('clicked');
			}
		} else if (action == 'out' && !$(event).hasClass('clicked')) {
			$(event).addClass('inactive');
			$(event).removeClass('current');
			$(event).parent().find('p#p-text-before').removeClass('inactive');
			$(event).parent().find('p#p-text-before').addClass('current');

			$('p#p-img-after').addClass('inactive');
			$('p#p-img-after').removeClass('current');
			$('p#p-img-before').removeClass('inactive');
			$('p#p-img-before').addClass('current');
		}
	}

	/* begin text "BEFORE" and "AFTER" actions (hover/click) */
	$('body.portfolio div#portfolio-toggle p#p-text-before').bind('mouseover', function(){
		portfolioBefore(this, 'over');
	}).bind('mouseout', function(){
		portfolioBefore(this, 'out');
	});
	$('body.portfolio div#portfolio-toggle p#p-text-before').bind('click', function(){
		portfolioBefore(this, 'clicked');
		return false;
	});

	$('body.portfolio div#portfolio-toggle p#p-text-after').bind('mouseover', function(){
		portfolioAfter(this, 'over');
	}).bind('mouseout', function(){
		portfolioAfter(this, 'out');
	});
	$('body.portfolio div#portfolio-toggle p#p-text-after').bind('click', function(){
		portfolioAfter(this, 'clicked');
		return false;
	});
	/* end actions */


	/* begin images "BEFORE" and "AFTER" actions (hover/click) */
	$('body.portfolio div.current-portfolio p#p-img-before').bind('mouseover', function(){
		if ($('body.portfolio div#portfolio-toggle p#p-text-before').hasClass('clicked'))
			$('body.portfolio div#portfolio-toggle p#p-text-after').trigger('mouseover');
	});
	$('body.portfolio div.current-portfolio p#p-img-after').bind('mouseout', function(){
		if ($('body.portfolio div#portfolio-toggle p#p-text-before').hasClass('clicked'))
			$('body.portfolio div#portfolio-toggle p#p-text-before').trigger('mouseover');
	});

	$('body.portfolio div.current-portfolio p#p-img-after').bind('mouseover', function(){
		if ($('body.portfolio div#portfolio-toggle p#p-text-after').hasClass('clicked'))
			$('body.portfolio div#portfolio-toggle p#p-text-before').trigger('mouseover');
	});
	$('body.portfolio div.current-portfolio p#p-img-before').bind('mouseout', function(){
		if ($('body.portfolio div#portfolio-toggle p#p-text-after').hasClass('clicked'))
			$('body.portfolio div#portfolio-toggle p#p-text-after').trigger('mouseover');
	});

	$('body.portfolio div.current-portfolio p#p-img-before').bind('click', function(){
		$('body.portfolio div#portfolio-toggle p#p-text-before').trigger('click');
		return false;
	});
	$('body.portfolio div.current-portfolio p#p-img-after').bind('click', function(){
		$('body.portfolio div#portfolio-toggle p#p-text-after').trigger('click');
		return false;
	});
	/* end actions */


});

