var selectCategories = new Array(); 
var selectRegions = new Array(); 

var selectCategoriesText = new Array(); 
var selectRegionsText = new Array(); 

$(document).ready(function(){ 
	
	var $search_textbox = $("#searchform #lan");
	var $search_regions = $("#search_regions");
	var $search_regions_list = $("#search_regions ul");
	var $search_region_item = $("#search_regions ul li a");

	var $search_region_item_value = $("#search_regions ul li a").text();
	
	$search_textbox.click(function(){
		if ( !$search_regions.is(":visible") )
			 $search_regions.show();//$search_regions.addClass("visible");
		else
			 $search_regions.hide();//$search_regions.removeClass("visible");
	});


	var hideregions = '';
	$('#regions').mouseout(function(){
		hideregions = setTimeout(function() {$('#regions-search-div').fadeOut('fast');}, 1000);
    }).mouseover(function(){
		clearTimeout(hideregions);
		$('#regions-search-div').show();
    });

	$('#regions-search-div').mouseout(function(){
		hideregions = setTimeout(function() {$('#regions-search-div').fadeOut('fast');}, 1000);
    }).mouseover(function(){
		clearTimeout(hideregions);
		$('#regions-search-div').show();
    });


	var hidecategories = '';
	$('#categories').mouseout(function(){
		hidecategories = setTimeout(function() {$('#category-search-div').fadeOut('fast');}, 1000);
    }).mouseover(function(){
		clearTimeout(hidecategories);
		$('#category-search-div').show();
    });

	$('#category-search-div').mouseout(function(){
		hidecategories = setTimeout(function() {$('#category-search-div').fadeOut('fast');}, 1000);
    }).mouseover(function(){
		clearTimeout(hidecategories);
		$('#category-search-div').show();
    });


	$search_textbox.keyup(function(){
		 $search_regions.show();//$search_regions.addClass("visible");
	});
	
	$search_region_item.click(function(){
		 $("input[name='l']").val($(this).text());
		 $search_regions.hide();//$search_regions.removeClass("visible");
		 return false;
	});
	
	//if ($search_regions.hasClass("visible")) {
	//	$search_regions.mouseout(function() {
	//		$search_regions.removeClass("visible");
	//	});
	//}
	var hideDelay = '';
	$search_regions.mouseout(function(){
      hideDelay = setTimeout(function() {$search_regions.fadeOut('fast');}, 2000);//$(this).removeClass("visible");
      
    }).mouseover(function(){
		clearTimeout(hideDelay);
		$search_regions.show();//$(this).addClass("visible");
    });

	//////////////////////////////////////////////////////////////////////////////////
	// Login From Gender
	
	$("#experience_category").change(function(){
		if (!$(this).val()) return;
		
		$.ajax({
		   type: "GET",
		   url: _WPURL+"/ajax",
		   data: "_action=subcat&_maincat="+$(this).val(),
		   success: function(data){
			 $("#selectexperience_subcategory").html('V\u00E4lj');
			 $("#experience_subcategory").html(data);
			 $("#experience_subcategory").val('');
		   }
		 });		
	});

	
	///////////////////////////////////////////////////////////////////////////////////

});

var MouseOutArray = function() { return [145,207,238]; };
var MouseOverArray = function() { return [179,219,238]; };
						
$(function() { 
	$('#box1').mouseover(function() {
		$(this).highlightFade({start:$(this).bgColor(),speed:500,end:MouseOverArray()});
	});
	$('#box1').mouseout(function() {
		$(this).highlightFade({start:$(this).bgColor(),speed:500,end:MouseOutArray()});
	});
	$('#box2').mouseover(function() {
		$(this).highlightFade({start:$(this).bgColor(),speed:500,end:MouseOverArray()});
	});
	$('#box2').mouseout(function() {
		$(this).highlightFade({start:$(this).bgColor(),speed:500,end:MouseOutArray()});
	});
	$('#box3').mouseover(function() {
		$(this).highlightFade({start:$(this).bgColor(),speed:500,end:MouseOverArray()});
	});
	$('#box3').mouseout(function() {
		$(this).highlightFade({start:$(this).bgColor(),speed:500,end:MouseOutArray()});
	});
});

$(function() { 
	$('#search_again').click(function(){
		$('#searchphrase').addClass("hidden");
		$('#search_content').fadeIn("slow");
	});
});

$(function() {
    $('form').each(function() {
        $('input').keypress(function(e) {
            // Enter pressed?
            if(e.which == 10 || e.which == 13) {
                this.form.submit();
            }
        });

        //$('input[type=submit]').hide();
    });
});

	
	
 /* Added by Aqeel Afzal : Start */
 /* Left column categories scroller */
 
 /* FOR CATEGORIES : START */
$(function() {
	$("#categories li").hover(
	  function () {$(this).addClass("bg-orange");},
	  function () {
	  		var itemId = $(this).attr('id');
		  	if ( jQuery.inArray(itemId, selectCategories) == -1 )
			  $(this).removeClass("bg-orange");
	  }
	);
	
	$('#categories li').click(function() {
		  	var itemId = $(this).attr('id');
			var inputVal = '#'+itemId+"-ck";
			var itemText = $(this).text();
			
		  	if ( itemId && jQuery.inArray(itemId, selectCategories) == -1 ){
				selectCategories.push(itemId);
				selectCategoriesText.push(itemText);
				$(inputVal).removeAttr("disabled"); 
				$(inputVal).val(itemId);
				
				$('#category-search').val( selectCategoriesText.toString() );
			}else if ( itemId ){
				selectCategories = jQuery.grep(selectCategories, function(value) {return value != itemId;	});	
				selectCategoriesText = jQuery.grep(selectCategoriesText, function(value) {return value != itemText;	});	
				$(inputVal).val("");
				$(inputVal).attr("disabled", true);
				
				$('#category-search').val( selectCategoriesText.toString() );
			}
			
			RefeshDiv(selectCategoriesText, 'category-search-div','removeCategoryLi');
	});	

		$("#categories").mousemove(function(e) {
										 
		 if($(this).height() < $("#categories-menu").height()) {
			var distance = e.pageY - $(this).offset().top;
			var percentage = distance / $(this).height();
			var targetY = -Math.round(($("#categories-menu").height() - $(this).height()) * percentage);
			
			if (targetY > -20 ){
				targetY=targetY+10;
			}
			$('#categories-menu').animate({top: [targetY+"px", "easeOutCirc"]}, { queue:false, duration:1000 });
		}
	});
 /* FOR CATEGORIES : END */

 /* FOR REGIONS : START */
	$("#regions li").hover(
	  function () {$(this).addClass("bg-orange");},
	  function () {
	  		var itemId = $(this).attr('id');
		  	if ( jQuery.inArray(itemId, selectRegions) == -1 )
			  $(this).removeClass("bg-orange");
	  }
	);
	
	$('#regions li').click(function() {
		  	var itemId = $(this).attr('id');
			var inputVal = '#'+itemId+"-ck";
			var itemText = $(this).text();
			
		  	if ( itemId && jQuery.inArray(itemId, selectRegions) == -1 ){
				selectRegions.push(itemId);
				selectRegionsText.push(itemText);
				
				$(inputVal).removeAttr("disabled"); 
				$(inputVal).val(itemId);
				
			}else if ( itemId ){
				selectRegions = jQuery.grep(selectRegions, function(value) {return value != itemId;	});	
				selectRegionsText = jQuery.grep(selectRegionsText, function(value) {return value != itemText;	});
				
				$(inputVal).val("");
				$(inputVal).attr("disabled", true);
			}
			RefeshDiv(selectRegionsText, 'regions-search-div','removeRegionsLi');
	});	

		$("#regions").mousemove(function(e) {
										 
		 if($(this).height() < $("#regions-menu").height()) {
			var distance = e.pageY - $(this).offset().top;
			var percentage = distance / $(this).height();
			var targetY = -Math.round(($("#regions-menu").height() - $(this).height()) * percentage);
			
			if (targetY > -20 ){
				targetY=targetY+10;
			}
			
			$('#regions-menu').animate({top: [targetY+"px", "easeOutCirc"]}, { queue:false, duration:1000 });
		}
	});
 /* FOR REGIONS : END */

 /* Added by Aqeel Afzal : End */
 
 // Bind for label on ul li so that you can set it as active. Have to check for ie because it handles input in labels different
 if ( $.browser.msie ) 
 {
	$("#sidebar ul li label").bind("click",function()
	 {
	 	if($(this).parent().hasClass("selected"))
	 	{
	 		$(this).parent().removeClass();
	 		$(this).find(":input").attr('checked', 'checked');
	 	}
	 	else
	 	{
	 		$(this).parent().addClass("selected");	
	 		$(this).find(":input").attr('checked', 'checked');
	 	}
	 });
 }
 else
 {
	 $("#sidebar ul li label input").bind("click",function()
	 {
	 	if($(this).parent().parent().hasClass("selected"))
	 	{
	 		$(this).parent().parent().removeClass();
	 	}
	 	else
	 	{
	 		$(this).parent().parent().addClass("selected");	
	 	}
	 });
 }
});


function RefeshDiv( itemArray, DivId, Removefunction )
{
	var divItems = $('#'+DivId+' ul');
	var content ='<li><div class="info">Klicka p&aring; regioner f&ouml;r att v&auml;lja</div></li>';
	var _alength = itemArray.length;
	for (i=0;i<_alength;i++)
	{
		content += '<li>';
		content += '<div class="button" onclick="'+Removefunction+'(this);"></div>';
		content += '<div class="text">'+itemArray[i]+'</div>';
		content += '</li>';
	}

	divItems.html(content);
}

function removeRegionsLi( eleDiv )
{
	var theLiText = jQuery.trim( $(eleDiv).closest('li').text() );
	var index = jQuery.inArray(theLiText, selectRegionsText);
	
	 $( '#'+selectRegions[index] ).removeClass("bg-orange");

	var inputVal = '#'+selectRegions[index]+"-ck";
	$(inputVal).val("");
	$(inputVal).attr("disabled", true);

	selectRegionsText.splice(index,1);
	selectRegions.splice(index,1);
	
	$(eleDiv).closest('li').remove();
}

function removeCategoryLi( eleDiv )
{
	var theLiText = jQuery.trim( $(eleDiv).closest('li').text() );
	var index = jQuery.inArray(theLiText, selectCategoriesText);
	
	$( '#'+selectCategories[index] ).removeClass("bg-orange");

	var inputVal = '#'+selectCategories[index]+"-ck";
	$(inputVal).val("");
	$(inputVal).attr("disabled", true);

	selectCategoriesText.splice(index,1);
	selectCategories.splice(index,1);
	
	$(eleDiv).closest('li').remove();
}

function clearall()
{
	for(var i in selectRegions)
	{
		 $( '#'+selectRegions[i] ).removeClass("bg-orange");
		 inputVal = '#'+selectRegions[i]+"-ck";
		$(inputVal).val("");
		$(inputVal).attr("disabled", true);
	}
	
	for(var i in selectCategories)
	{
	 $( '#'+selectCategories[i] ).removeClass("bg-orange");
		 inputVal = '#'+selectCategories[i]+"-ck";
		$(inputVal).val("");
		$(inputVal).attr("disabled", true);
	}
	
	selectRegions = [];
	selectRegionsText = [];
	selectCategories = [];
	selectCategoriesText = [];
	
	RefeshDiv(selectCategoriesText, 'category-search-div');
	RefeshDiv(selectRegionsText, 'regions-search-div');
}	

function toggleDiv( _id, _type )
{
	_pLi = $('#categories_root_level li');
	_pDiv = $('#show-more-'+_id).parent('.children').get(0);
	_pDivH = $(_pDiv).height();
	
	if(_type == 'f')
	{
		 $('#show-more-'+_id).hide();
		 $('#sub-cat-'+_id).show();
		 $('#show-few-'+_id).show();
	}
	else if(_type == 'm')
	{
		 $('#show-few-'+_id).hide();
		 $('#sub-cat-'+_id).hide();
		 $('#show-more-'+_id).show();
	}
}

function inputOnBlur( _ele, _txt )
{
	_val = $(_ele).val();
	
	if ( _val == '')
	  $(_ele).val(_txt);
}

function inputOnFocus( _ele, _txt )
{
	_val = $(_ele).val();
	
	if ( _val == _txt)
	  $(_ele).val('');
}


//////////////////////////////////////////////////////////////////////////////////
// Login From Gender
function delFile ( _type, _ele)
{
	if ( confirm("\u00C4r du s\u00E4ker p\u00E5 att du vill radera denna fil?") )
	{
		$('#'+_type+'_del').val('1');
		$('#'+_type+'_file').html('');
		$(_ele).remove();
	}
}
// Login From Gender End

function redirect( _url )
{
	window.location.href = _url;
}

function toggleDisplay( _ele, _obj )
{
	$element = $('#'+_ele);
	if ( !$element.is(":visible") )
	{
		 $element.show();
		 $(_obj).css("backgroundPosition", "5px -7px"); 
		 $(_obj).css("backgroundColor", "#CDE0E9"); 
	}
	else
	{
		 $element.hide();
		 $(_obj).css("backgroundPosition", "5px 7px"); 
		 $(_obj).css("backgroundColor", "#FFFFFF"); 
	}
}

function getMedia ( eId, URL )
{

	$.ajax({
   type: "GET",
   url: URL,
   success: function(data){
		$("#"+eId).html(data);
		//alert(data);
	   }
	 });
}

function getMediaAuthor ( eId, author )
{
	URL = getLink(author);
	
   $.ajax({
   type: "GET",
   url: URL,
   success: function(data){
		$("#"+eId).html(data);
		//alert(data);
	   }
	 });
}

function getLink(author)
{
	if ( author == 'KrodsheradKommune' )
		return "http://www.toppjobbet.no/media/?t=ss&a=KrodsheradKommune&_w=400&_h=267";
	else if( author == 'TanaKommune' )
		return "http://www.toppjobbet.no/media/?t=ss&a=tana&_w=400&_h=267";
	else if( author == 'vestvagoykommune' )
		return "http://www.toppjobbet.no/media/?t=ss&a=vestvagoy&_w=400&_h=267";
}


