﻿var case_PageTitle = "";
var originalPageTitle = "";

$(document).ready(function()
{
	//
	// autoscroll
    window.scrollTo(0, 0);
    $.scrollTo.defaults.axis = 'xy';
    
    //
	// get page title
	originalPageTitle = document.title + "";
	case_PageTitle = originalPageTitle.replace("Kundprojekt", "");
	
	//
	// check hash
	var hash = window.location.hash;
	if (hash != "" && false)
	{
		//
		// remove first char because it is '#';
		hash = hash.substr(1);
		
		try
		{
			var fnc = eval("hash_Handle_" + hash.substr(0, 2));
			if (typeof(fnc) == "function")
			{
				fnc(hash);
			}
		}catch(e){}
	}
	
	if ($('#CommentatorText').length == 1)
	{
		intializeRobTextboxes('CommentatorText', 'Email');
	}
	if ($('#PhoneNumber').length == 1)
	{
			intializeRobTextboxes('PhoneNumber', 'Name');
	}
	
	$('#writeComment').click(writeComment_Click);
});

function writeComment_Click()
{
	var cf = $('#commentFields');
	var h = cf.children('table').height() + 10;
	cf.animate({height: h}, "normal");
	$(this).text("SKRIV KOMMENTAR:");
	
	//$('#wrapper').scrollTo($("#commentFields"));
}

function intializeRobTextboxes()
{
	var foc = function()
	{
		var elem = $(this);
		var t = elem.attr("rel");
		
		if (elem.val() == t)
		{
			elem.val("");
		}
	};
	
	var blu = function()
	{
		var elem = $(this);
		var t = elem.attr("rel");
		
		if (elem.val() == "")
		{
			elem.val(t);
		}
	};
	
	for (var i = 0; i < arguments.length; i++)
	{
		var e = arguments[i];
		if (typeof(e) == "string")
			e = $('#' + e);
			
		e.focus(foc).blur(blu);
	}
}

function hash_Handle_kp(hash)
{
	var id = hash.substr(3);
	
	showCase(id);
}

function hash_Handle_bp(hash)
{
	var id = hash.substr(3);
	
	showBlogPost(id);
}

function scrollBack()
{
	document.title = originalPageTitle;
	$.scrollTo({top: 0, left: 0}, 1000, { easing: 'easeInOutQuad' });
}

function setCaseScrolling()
{
	//
	// get screen width
	var w = $(window).width();
	
	window.scrollTo(w, 0);
}

function case_ChangeImage(p, o)
{
	$('#caseViewer a.selected').removeClass('selected');
	$(o).addClass("selected");
	$('#case_ImageViwerDiv').scrollTo($('#i_' + p), 500);
}

function case_PrevImage()
{
	var o = $('#caseViewer a.selected');
	var a = $('.case_ImageViewer a.imageSelector');
	
	var i = a.index(o.get(0));
	if (i == 0)
	{
		i = a.length;
	}
	
	i--;
	
	var imageId = $(a[i]).attr("rel");
	case_ChangeImage(imageId, a[i]);
}

function case_NextImage()
{
	var o = $('#caseViewer a.selected');
	var a = $('.case_ImageViewer a.imageSelector');
	
	var i = a.index(o.get(0));
	i++;
	
	if (i == a.length)
	{
		i = 0;
	}
	
	var imageId = $(a[i]).attr("rel");
	case_ChangeImage(imageId, a[i]);
}

function showCase(caseId)
{
	//
	// get screen width
	var w = $(window).width();
	
	//
	// we want the element centered
	var c = (w + (w / 2)) - ($('#caseViewer').width() / 2);
	
	//
	// move element
	$('#caseViewer').css('left', c);
	
	//$('#caseViewer').append('<img src="images/case-loader.gif" style="position: absolute; top:290px; left: 706px;" alt="loading..." />');
	$('#caseViewer').fadeTo("normal", 0.7);
	
	//
	// ajax-load
	$.get('kundprojekt/' + caseId, {ajax: 'true', rd: Math.floor(Math.random()*1111212121)}, function(d)
	{
		$('#caseViewer').html(d);
		$('#caseViewer').fadeTo("normal", 1.0);
	});
	
	//
	// scroll
	$.scrollTo({top: 0, left: w}, 1000, { easing: 'easeInOutQuad' });
	
	//$('.container').animate({left: -1000}, 1000);
	//$('#caseViewer').animate({left: 0}, 1000);
}

function case_SetTitle(t)
{
	document.title = t + case_PageTitle;
}

function hideOpenBlogPost()
{
	$('.blog_PostContent').each(function()
	{
		var e = $(this);
		
		if (e.height() > 100)
		{
			e.animate({height: 100}, "slow");
		}
	});
}


function showBlogPost(id)
{
	hideOpenBlogPost();
	
	var o = $('#bp_' + id);
	var hh = o.children('.blog_post_heighthelper');
	
	if (o.height() == hh.height())
	{
		o.animate({height: 100}, "slow");
	}
	else if(o.height() == 100)
	{	
		$.get('blog/1/' + id + '/comments', {}, function(d)
		{
			$('#blog_Clicks_' + id).text(d);
		});
		
		o.animate({height: hh.height()}, "slow", function()
		{
			$('.wrapper').scrollTo({top: parseInt(o.parent().offset().top) - 20, left: 0}, 500);
		});
	}
}