var track_events = false;
var blank = new Image();
blank.src = '/images/blank.gif'

$(document).ready(function() {  
	
	if (track_events) {
		full_page_name = page_name.toString() + "/1";
		pageTracker._trackEvent('Project image', 'View', full_page_name);
	}

	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) {
		// get all pngs on page
		$('img[src$=.png]').each(function() {
			if (!this.complete) {
				this.onload = function() { fixPng(this) };
			} else {
				fixPng(this);
			}
		});
	}
	
	$('.email').click(function(){ 
		fake_string = $(this).attr('href');
		email_arr = fake_string.split('/',3);
		$(this).attr('href','mailto:' + email_arr[1] + '@' + email_arr[2]);
	});
		
	$('#left-arrow').click(function(){
		current_left = parseInt($('#image').css("margin-left").replace("px", ""));
		new_left = current_left + 900;
		$('#image').animate({marginLeft:new_left}, 1000);
		old_image = this_image;
		new_image = this_image - 1;
		$('#description_' + old_image).fadeOut(450);
		setTimeout(function() {
			$('#description_' + new_image).fadeIn(500);
		}, 500);
		this_image -= 1;
		$('#menu').animate({color: '#' + $('#color_'+this_image).val()}, 500);	
		decideArrowVisibility();
		trackClick();
	});
	
	$('#right-arrow').click(function(){  
		current_left = parseInt($('#image').css('margin-left').replace('px', ''));
		new_left = current_left - 900;
		$('#image').animate({marginLeft:new_left}, 1000);
		old_image = this_image;
		new_image = this_image + 1;
		$('#description_' + old_image).fadeOut(450);
		setTimeout(function() {
			$('#description_' + new_image).fadeIn(500);
		}, 500);
		this_image += 1;
		$('#menu').animate({color: '#' + $('#color_'+this_image).val()}, 500);		
		decideArrowVisibility();
		trackClick();
	}); 
	           
	
	$('.tile-arrow-right').click(function() {
		
		current_left = parseInt($(this).parent().parent().find('.tile-strip').css('margin-left').replace('px', ''));
		total_tiles = $(this).parent().parent().find('.tile-strip').children().size();
		remaining_tiles = (total_tiles) - (((current_left * -1) / 94) + 6);
		
		if (remaining_tiles > 6) {
			new_left = current_left - (94 * 6);
		} else {
			new_left = current_left - (94 * remaining_tiles);			
		}

		$(this).parent().parent().find('.tile-strip').animate({marginLeft:new_left}, 1000);
		setTimeout(function() {
			decideMiniArrowVisibility();
		}, 1100);
				
	});
	
	$('.tile-arrow-left').click(function() {
		
		current_left = parseInt($(this).parent().parent().find('.tile-strip').css('margin-left').replace('px', ''));
		total_tiles = $(this).parent().parent().find('.tile-strip').children().size();
		remaining_tiles = (((current_left * -1) / 94));
		
		if (remaining_tiles > 6) {
			new_left = current_left + (94 * 6);
		} else {
			new_left = current_left + (94 * remaining_tiles);			
		}

		$(this).parent().parent().find('.tile-strip').animate({marginLeft:new_left}, 1000);
		setTimeout(function() {
			decideMiniArrowVisibility();
		}, 1100);
				
	});
	              
	decideArrowVisibility();
	decideMiniArrowVisibility();
	
});

function decideArrowVisibility() {
	if (page_type == 'project_page') {
		if (this_image > 1) {
			$('#left-arrow').show();
			$('#left-arrow').animate({color: '#' + $('#color_'+this_image).val()}, 500); 
		} else {
			$('#left-arrow').hide();
		}
		if (this_image < number_of_images) {
			$('#right-arrow').show();
			$('#right-arrow').animate({color: '#' + $('#color_'+this_image).val()}, 500); 
		} else {
			$('#right-arrow').hide();
		}
		
	}
} 

function decideMiniArrowVisibility() {
	if (page_type == 'project_list') {
		$('.tile-holder').each(function() {
			/*if ($(this).find('.tile-strip').children().size() <= 6) {
				$(this).parent().find('.tile-arrows').find('.tile-arrow-right').hide();
				$(this).parent().find('.tile-arrows').find('.tile-arrow-left').hide();
			}*/
			current_left = parseInt($(this).find('.tile-strip').css('margin-left').replace('px', ''));
			total_tiles = $(this).find('.tile-strip').children().size();
			left_tiles = (((current_left * -1) / 94));
			right_tiles = (total_tiles) - (((current_left * -1) / 94) + 6);
			//alert('left_tiles: ' + left_tiles);
			//alert('right_tiles: ' + right_tiles);
			if (right_tiles <= 0) {
				$(this).parent().find('.tile-arrows').find('.tile-arrow-right').hide();
			} else {
				$(this).parent().find('.tile-arrows').find('.tile-arrow-right').show();
			}
			
			if (left_tiles <= 0) {
				$(this).parent().find('.tile-arrows').find('.tile-arrow-left').hide();
			} else {
				$(this).parent().find('.tile-arrows').find('.tile-arrow-left').show();
			}
		});	
	}
}

function trackClick() {
	if (track_events) {
		//alert('track');
		full_page_name = page_name + "/" + this_image;
		//alert(full_page_name);
                pageTracker._trackEvent('Project image', 'View', full_page_name);
        }
}

function fixPng(png) {
	// get src
	var src = png.src;
	// set width and height
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	// replace by blank image
	png.onload = function() { };
	png.src = blank.src;
	// set filter (display original image)
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
                   

