// Globals
var searchText = 'Skriv sökord här:';
var filterText = 'Sök på namn eller titel:'
var errorText = 'Fyll i ett sökord här först!';
//
//
$(function() {
	fadeInBoxes();
	initSearchBox();
	initSlider();
	initContactFilter();
	//$('.imgCont a').each(function(i){$(this).css('background-color', randomColor())});
});

//contaxt page ajax
function initContactFilter (){
	$("#filterField").val(filterText);
	// focus
	$("#filterField").focus(function () { 
		if ($(this).val() == filterText) {$(this).val('')}; 
	});
	$("#filterField").blur(function () { 
		if ($(this).val() == '') {$(this).val(filterText)}; 
	});
	
	$('#filterField').bind('textchange', function () {
		findContacts($(this).val());
	});
	$('select').change(function () {
		findContacts($('#filterField').val());
	});

	$('#sort').bind($.browser.msie ? 'click' : 'change', function(event) {
		findContacts($('#filterField').val());
	})
}

var latestSearchStr = '';

function findContacts(searchStr){
	loaded = false;
	searchStr = (searchStr == filterText)?'':searchStr;
	latestSearchStr = searchStr;
	//loopLoader();
	//$('#loading').show();
	//$('#loading').removeClass('loading').addClass('loadMore');
	
	var sort = ($("input[@name=sort]:checked").attr('id') == 'rand')?'rand':'asc';	
	
	if (searchStr.length >= 2 || searchStr.length == 0) {
		$.get('/?isAjax=true&searchStr='+searchStr+'&sort='+sort, function(data) {
			
			if ($('#searchStr', data).html() == latestSearchStr) {
				$('#results').html(data);
				$('#searchStr').hide();
				fadeInBoxes();
			};
			
			//loaded = true;
			//$('#loading').hide();

		}, 'html');
	};
     
}


function loopLoader (){
	if(!loaded){
		window.setTimeout(function() {
		 $('#loading').html(loadingSequence.charAt(loopCnt)+((inited) ? ' <span class="grey">'+(postsOffset+1)+'-'+(postsOffset+numPosts)+'</span>' : ''));
		loopCnt++;
		if (loopCnt >= loadingSequence.length) {
			loopCnt = 0;
		};
		loopLoader();
		}, 200);
	}
}


function fadeInBoxes(){
	$('.smallBox').each(function(i) {
		var _this = this;
		window.setTimeout(function(){ setVisible(_this)	}, i*50);		
	});
}

function setVisible (item) {
	//$(item).css('visibility', 'visible');
	$(item).fadeIn(200);
}

function initSearchBox(){
	if ($('#searchField').val() == '') {
		$("#searchField").val(searchText);
		$("#searchField").css({opacity: 0});
	} else {
		$("#searchField").css({opacity: 1, display: 'block'});
		$("#searchsubmit").css({backgroundColor: '#ececec'});
	};
	
	$('#searchheader').hover(
		function (){
			fadeInSearchfield();
		},
		function (){
			if (/*$('#searchField').val() == '' ||*/ $('#searchField').val() == searchText || $('#searchField').val() == errorText) {
				fadeOutSearchfield();
			};	
		}	
	);
	
	// evaluate
	$('#searchform').submit(function(event) {
		if ($("#searchField").val() == searchText || $('#searchField').val() == errorText) {
			$("#searchField").val(errorText);
			fadeInSearchfield();
			event.preventDefault();
			}; 
	});
	
	// focus
	$("#searchField").focus(function () { 
		if ($(this).val() == searchText || $('#searchField').val() == errorText) {$(this).val('')}; 
	});
	$("#searchField").blur(function () { 
		if ($(this).val() == '') {$(this).val(searchText)}; 
		if ($(this).val() == searchText) {
			fadeOutSearchfield();
		};

	});
}

function fadeInSearchfield(){
	$('#searchField').stop();
	$("#searchField").show().animate({ 
	        opacity: 1
	      }, 500 );
	$('#searchsubmit').stop();
	$("#searchsubmit").animate({ 
	        backgroundColor: '#ececec'
	      }, 500 );
}

function fadeOutSearchfield (){
	$('#searchField').stop();
	$("#searchField").animate({ 
	        opacity: 0
	      }, 500);
	$('#searchsubmit').stop();
	$("#searchsubmit").animate({ 
	        backgroundColor: '#ffffff'
	      }, 500 );
}

// SLIDER
$move_by = 940;
$frame_left = 0;
$frame_no = 1;
$start_middle = 0;

function initSlider(){
	$max_clicks = $("#image_container").children().size(); 	
	setImageCounter();
	if ($max_clicks > 1){
		$('#sliderNav').css({display: 'block'});
	} else {
		$('#prev').remove(); 	
		$('#next').remove();
	}
	/* Kudos to Rob MacKay [http://www.twitter.com/svgrob] for suggesting that we don't have to put a static style="width: 3960px" attribute in the div tag */
	$imgCont = $move_by * $max_clicks; 
	
	$("#image_container").css({width : $imgCont});
	
	if($start_middle == 1)
		{
			// Get the middle frame, according to the $max_clicks
			$new_frame_no = ($max_clicks/2).toFixed(0)
			
			// Adjust the frame position
			$new_left = -($new_frame_no * $move_by);						
			$new_left_attr = $new_left+"px";
			
			// Do the move
			$("#image_container").animate({left: $new_left_attr}, 800 );
			
			// Save the new values
			$frame_left = $new_left;
			$frame_no = ($new_frame_no/1 + 1);
		}
	
	$(".prev").click(function()
		{
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)-1);
			$new_left = (($frame_left/1) + $move_by);
			/* Check if we're moving too far over */
			if($new_frame_no <= 0)
				{
					/* Move the images all the way left, minus one frame */
					$new_left = -($move_by*$max_clicks)+$move_by;
					$new_frame_no = $max_clicks;
				}
			$new_left_attr = $new_left+"px";
			$("#image_container").animate({left: $new_left_attr}, 800 );
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			setImageCounter();
		});
	$(".next").click(function()
		{
			/* Set the new position & frame number */
			$new_frame_no = (($frame_no/1)+1);
			$new_left = (($frame_left/1) - $move_by);
			/* Check if we're moving too far over */
			if($new_frame_no > $max_clicks)
				{		
					/* Move all the way right, to the beginning*/
					$new_left = 0;
					$new_frame_no = 1;
				}				
			$new_left_attr = $new_left+"px";
			$("#image_container").animate({left: $new_left_attr}, 800 );
			$frame_left = $new_left;
			$frame_no = $new_frame_no;
			setImageCounter();
		});
}

function fadeIn(obj){
	$(obj).stop();
	$(obj).show().animate({ 
	        opacity: 1
	      }, 500 );
}

function fadeOut(obj){
	$(obj).stop();
	$(obj).animate({ 
	        opacity: 0.1
	      }, 500);
}

function setImageCounter(){
	$('#imageCounter').text($frame_no + '/' + $max_clicks);
}

function randomColor() {
	var hex=new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
	var clr="#";
	for (var i=0; i < 6; i++) clr+=hex[Math.floor(Math.random()*hex.length)];
	return clr;
}
