// Internet Explorer 6
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
// exactly Internet Explorer 7
var IE7 = false /*@cc_on || @_jscript_version == 5.7 @*/;
// at least Internet Explorer 7
var gteIE7 = false /*@cc_on || @_jscript_version >= 5.7 @*/;
// any Internet Explorer (thanks to Dean)
var isMSIE = /*@cc_on!@*/false;


function toggleHScroll() {
	if (horizScroll==true) {
		horizScroll = false;
	} else {
		horizScroll = true;
	}
	$("#toggle-h-scroll").toggleClass("disabled");
	return false;
}

var currentScroll = 0;
if (previous_scroll>0) {
	currentScroll = previous_scroll;
	saveCurrentScroll();
}
var scrolling = false;
var scrollingSpeed = 300;
var scrollingSpeedFar = 600;
function scrollToLeft() {
	if (scrolling==false) {
		scrolling = true;
		$.scrollTo('-=314px', {speed:scrollingSpeed}, {axis:'x'});
		setTimeout(function(){ scrolling = false; }, scrollingSpeed);
		
		// Save scroll
		currentScroll -= 314;
		setTimeout(saveCurrentScroll, scrollingSpeed);
	}
}
function scrollToRight() {
	if (scrolling==false) {
		scrolling = true;
		$.scrollTo('+=314px', {speed:scrollingSpeed}, {axis:'x'});
		setTimeout(function(){ scrolling = false; }, scrollingSpeed);
		
		// Save scroll
		currentScroll += 314;
		setTimeout(saveCurrentScroll, scrollingSpeed);
	}
}
function scrollToFarLeft() {
	if (scrolling==false) {
		scrolling = true;
		$.scrollTo('0%', {speed:scrollingSpeedFar}, {axis:'x'});
		setTimeout(function(){ scrolling = false; }, scrollingSpeedFar);
		
		// Save scroll
		currentScroll = 0;
		setTimeout(saveCurrentScroll, scrollingSpeed);
	}
}
function scrollToFarRight() {
	if (scrolling==false) {
		scrolling = true;
		$.scrollTo('100%', {speed:scrollingSpeedFar}, {axis:'x'});
		setTimeout(function(){ scrolling = false; }, scrollingSpeedFar);
		
		// Save scroll
		currentScroll = $("#wrapper").innerWidth();
		setTimeout(saveCurrentScroll, scrollingSpeed);
	}
}



/* DOCUMENT READY */
$(document).ready(function(){
	
	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"
	
	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);
	
	// non-clickable links
	$(".return-false").click( function() {
		return false;
	});
	
	
	// Jump Menu
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	
	// Clear Inputs
	$("#form-newsletter input, #form-search input").each(function(i){
		var inputValue = $(this).val();
		
		$(this).focus(function(){ 
			if ($(this).val()==inputValue) {
				$(this).val("");
			}
		});
		$(this).blur(function(){ 
			if ($(this).val()=="") {
				$(this).val(inputValue);
			}
		});
	});
	
	
	// Tooltip
	$("a.tooltip").tooltipLink();
	
	
	// Fading Link
	$(".fadingLink").fadeTo(300, 0.5);
	$(".fadingLink").hover(
		function(){
			$(this).fadeTo(1, 1);
		},
		function(){
			$(this).fadeTo(1, 0.5);
		}
	);
	
	
	/* GALLERY */
	/*$("#gallery-listing .img-thumb, #news .img-thumb").hover(
		function(){
			$(this).append("<span></span>");
		},
		function(){
			$("span", this).remove();
		}
	);*/
	$("#gallery-listing a.img-thumb, #news a.img-thumb").hover(
		function(){
			$("img", this).fadeTo(100, 0.85);
		},
		function(){
			$("img", this).fadeTo(100, 1);
		}
	);
	
	
	
	/* HORIZONTAL SCROLLING */
	if (!IE6 && horizScroll==true) {
		$("#wrapper").append('<div id="nav-but"></div>');
		//$("#nav-but").append('<a href="#" id="toggle-h-scroll" onclick="toggleHScroll(); return false;" class="tooltip" title="Toggle horizontal mouse scroll">Toggle horizontal mouse scroll</a>');
		$("#nav-but").append('<a href="#" id="scrollFarLeft" onclick="scrollToFarLeft(); return false;" class="tooltip" title="Scroll all the way left" rel="nofollow">Scroll all the way left</a>');
		$("#nav-but").append('<a href="#" id="scrollLeft" onclick="scrollToLeft(); return false;" class="tooltip" title="Scroll left" rel="nofollow">Scroll left</a>');
		$("#nav-but").append('<a href="#" id="scrollRight" onclick="scrollToRight(); return false;" class="tooltip" title="Scroll right" rel="nofollow">Scroll right</a>');
		$("#nav-but").append('<a href="#" id="scrollFarRight" onclick="scrollToFarRight(); return false;" class="tooltip" title="Scroll all the way right" rel="nofollow">Scroll all the way right</a>');
		$("a.tooltip").tooltipLink();
		
		$("#toggle-h-scroll, #scrollFarLeft, #scrollFarRight").fadeTo(500, 0.3);
		$("#scrollLeft, #scrollRight").fadeTo(500, 0.7);
		
		$("#toggle-h-scroll, #scrollFarLeft, #scrollFarRight").hover(function(){ $(this).fadeTo(100, 0.6); }, function(){ $(this).fadeTo(100, 0.3); });
		$("#scrollLeft, #scrollRight").hover(function(){ $(this).fadeTo(100, 1); }, function(){ $(this).fadeTo(100, 0.7); });
	}
	
	// default scroll to bottom/left
	if (horizScroll==true) {
		$.scrollTo({top:3000, left:previous_scroll}, {speed:500});
	}
	
	// scroll to bottom/left when window is resized
	$(window).resize(function(){
		if (horizScroll==true) {
			$.scrollTo({top:3000, left:previous_scroll}, {speed:500});
		}
	});
	
	// horizontal mouseWheel scroll
	$('body').mousewheel(function(event, delta) {
		if (horizScroll==true) {
			if (delta > 0) {
				$.scrollTo('-=80px', {speed:0}, {axis:'x'});
				currentScroll -= 80;
			}
			else if (delta < 0) {
				$.scrollTo('+=80px', {speed:0}, {axis:'x'});
				currentScroll += 80;
			}
			
			// Save scroll
			saveCurrentScroll();
		}
		//return false; // prevent default
	});
	
	
	// Font replacement
	replaceFonts();
	
	
	// Hide scrollbars
	$('body#index, body#gallery').css("overflow-y", "hidden");
	
	
	// Details buttons
	$("#menu-options a").click(function(){
		$("#content-info, #content-comments, #content-add-comment").hide();
		$("#menu-options li").removeClass("active");
		$(this).parent().addClass("active");
	});
	$("#link-info").click(function(){
		$("#content-info").fadeIn(300);
		saveCurrentSection("photo-info");
		return false;
	});
	$("#link-view-comments").click(function(){
		/*$('.scrollElementComments').jScrollPane({
			scrollbarWidth: "11",
			showArrows: true,
			arrowSize: "15",
			dragMinHeight: "19",
			dragMaxHeight: "19"
		});*/
		$("#content-comments").fadeIn(300);
		saveCurrentSection("view-comments");
		return false;
	});
	$("#link-post-comment").click(function(){
		$("#content-add-comment").fadeIn(300);
		saveCurrentSection("post-comment");
		return false;
	});
	
	$("#content-details .link-close").click(function(){
		$("#content-info, #content-comments, #content-add-comment").fadeOut(300);
		$("#menu-options li").removeClass("active");
		saveCurrentSection("");
		return false;
	});
	
});



// Save current scroll
function saveCurrentScroll() {
	$.ajax({
	    type: "GET",
	    url: "save-current-scroll.php",
	    data: {
	        current_scroll: currentScroll
	    },
		dataType: 'json',
	    success: function(data){
	    }
	});
	//alert(currentScroll);
}



// Save current section
function saveCurrentSection(section) {
	$.ajax({
	    type: "GET",
	    url: "save-current-section.php",
	    data: {
	        section: section
	    },
		dataType: 'json',
	    success: function(data){
	    }
	});
	
	var oldNext = $("#link-next").attr("href");
	var newNext = oldNext.replace(currentSection, section);
	$("#link-next").attr("href", newNext);
	
	var oldPrev = $("#link-prev").attr("href");
	var newPrev = oldPrev.replace(currentSection, section);
	$("#link-prev").attr("href", newPrev);
	
	currentSection = section;
	//alert(currentScroll);
}


// Font replacement
function replaceFonts() {
	Cufon.replace('button.submitButton', { fontFamily: 'Qlassik', hover: true, hoverables: { a: true, span: true, button: true } });
	Cufon.replace('#content-details h1, #content .h1-listing h1, #news h1, .replace', { fontFamily: 'Qlassik', hover: true, hoverables: { a: true } });
}
