$(document).ready(function() {
		
	$('#morefiles').click(function() {
		var many = $("#howmany").val();
		$("#morefilesdiv").load("voices.php #morefilesdiv > *", { morefiles : "set", howmany : many });
		return false;
	});
		
	$('.a_link')
		.live('click', function() {
			
			var url = $(this).attr("href");
			var target = "#"+$(this).attr("rev");
				$(".kontakt_content").prepend('<img id="extraloader" src="img/ajax-loader.gif" />');
				$(target).load(url+' '+target+' > *', function() {
				var href = $("#div_hold_currentPage").html().replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&");
				$('#searchresult').load(href+' table', function() {
					setTooltip();
					$("#extraloader").remove();
				});
			});
			
			return false;
			
		});

	$("#kon_tab_all a").live("click", function() {
		$(this).parent().removeClass("current-all").addClass("current-all_current");
		$("#kon_tab_men").removeClass("current-men_current").addClass("current-men");
		$("#kon_tab_women").removeClass("current-kvinna_current").addClass("current-kvinna");
	});
	
	$("#kon_tab_men a").live("click", function() {
		$(this).parent().removeClass("current-men").addClass("current-men_current");
		$("#kon_tab_all").removeClass("current-all_current").addClass("current-all");
		$("#kon_tab_women").removeClass("current-kvinna_current").addClass("current-kvinna");
	});
	
	$("#kon_tab_women a").live("click", function() {
		$(this).parent().removeClass("current-kvinna").addClass("current-kvinna_current");
		$("#kon_tab_men").removeClass("current-men_current").addClass("current-men");
		$("#kon_tab_all").removeClass("current-all_current").addClass("current-all");
	});	
	
	var $toolbar = $("#footer_toolbar");
	var toolHeight = $toolbar.height();
	var toolWidth = $toolbar.width();
	var	x = document.body.clientWidth / 2;
	var	y = document.body.clientHeight;
	$toolbar.css({
		"top": y,
		"left": (x-toolWidth/2) - 1,
		"width": toolWidth,
		"height": toolHeight,
		"position": "fixed",
		"zIndex" : "100"
	});
	
	$(window).resize(function() {
		toolHeight = $toolbar.height();
		toolWidth = $toolbar.width();
		x = document.body.clientWidth / 2;
		y = document.body.clientHeight;
		
		if ($toolbar.hasClass("isPlaying")) {
			$toolbar.css({
				"top": y-toolHeight,
				"left": (x-toolWidth/2) - 1,
				"width": toolWidth,
				"height": toolHeight,
				"position": "fixed",
				"zIndex" : "100"
			});	
		} else {
			$toolbar.css({
				"top": y,
				"left": x-toolWidth/2,
				"width": toolWidth,
				"height": toolHeight,
				"position": "fixed",
				"zIndex" : "100"
			});	
		}

	});
	
	$(".activate a").live("click", function() {
		$(".activate").removeClass("currentlang");
		$(this).parent().addClass("currentlang");
	});
	$("body").append('<div id="response_data"></div');
	$("#response_data").hide();
	$("#language_list .load").click(function() {
		$("#kon_tab_all").removeClass("current-all").addClass("current-all_current");
		$("#kon_tab_men").removeClass("current-men_current").addClass("current-men");
		$("#kon_tab_women").removeClass("current-kvinna_current").addClass("current-kvinna");
	});
	$('.load')
		.live('click', function() {

			var target = $(this).attr("rev");
			
			var searchresult_url = "js/searchresult.php?"+target+' #sort-table > *';
			var searchresult_tabs_url = "js/searchresult_tabs.php?"+target;
						
			$("#preloadergif").html('<img src="img/ajax-loader.gif" />');
			$("#searchresult  #sort-table").load(searchresult_url, function() {
				setTooltip();
				$("#preloadergif").html('');
			});
			
			$("#searchresult_tabs").load(searchresult_tabs_url, function() {
				setTooltip();
				$("#preloadergif").html('');
				
				var href = $("#div_hold_currentPage").html().replace("js/searchresult.php?", "").replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&");
								
				$("#kon_tab_all a").attr("rev", href.replace("&k=1", "&k=3").replace("&k=2", "&k=3"));
				$("#kon_tab_men a").attr("rev", href.replace("&k=3", "&k=1").replace("&k=2", "&k=1"));
				$("#kon_tab_women a").attr("rev", href.replace("&k=3", "&k=2").replace("&k=1", "&k=2"));
			});
						
			var destination = $("#menu").offset().top;
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 800);
			
			return false;
			
		});
	
		
	$(".anchorLink").click(function() {
		 $.ajax({
		   type: "POST",
		   url: "js/searchresult.php",
		   data: "reset=1"
		 });

	});
	$("tr .play_sound").live("click", function() { 
		var $parent = $(this).parent().parent();
		var target = $(this).attr("rev");

		var href = $("#div_hold_currentPage").html().replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&")+"&currentPlay="+target;
		
		/*
		$('tr.isPlaying').removeClass('isPlaying').find('td').removeAttr('style');
		
		$parent.find('td').attr('style','background-position: 0px -29px; color: rgb(255, 255, 255);');
		
		$parent.find('.star').attr('src', 'img/save_first_women_current.gif');
		
		$toolbar.animate({
			top: y-toolHeight
		}, function() {
			$parent.addClass("isPlaying");
		});*/
		
		
		$('#searchresult').load(href, function() {
			setTooltip();
			$toolbar.animate({
				top: y-toolHeight
			}, function() {
				$(this).addClass("isPlaying");
			});
		});
				
		return false;
	});
	
	$("#formFriend").hide();
	$("#radio_showfriend").live("click", function() {
		$("#formOffer").hide();
		$("#formFriend").show();
	});
	
	$("#radio_showoffer").live("click", function() {
		$("#formFriend").hide();
		$("#formOffer").show();
	});	
	
	$("#submitOffer")
		.live("click", function() {
			
			var $this = $(this);
			$this.attr("disabled", "disabled");
			
			var datastring = $("#formOffer").serialize();
			datastring += "&"+$(this).attr("name")+"=set";
			
			$(".my_favs").load("about.php .my_favs > *", datastring, function() {
				$this.removeAttr("disabled");
				$("#radio_showoffer").trigger('click');
			});


			return false;
		});
		
	$("#submitFriend")
		.live("click", function() {
			
			var $this = $(this);
			$this.attr("disabled", "disabled");
			
			var datastring = $("#formFriend").serialize();
			datastring += "&"+$(this).attr("name")+"=set";
			
			$(".my_favs").load("about.php .my_favs > *", datastring, function() {
				$this.removeAttr("disabled");
				$("#radio_showfriend").trigger('click');
			});
			
			return false;
		});		
	
	$("#clearMyFavs").live("click", function() {
		var target = $(this).attr("href");
		var href = $("#div_hold_currentPage").html().replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&").replace("&amp;", "&");
		
		$("#fav_table").load(target+" #fav_table > *");
		$('#searchresult').load(href+' table', function() {
			setTooltip();
		});
		$("#radio_showoffer").trigger('click');
		return false;
	});	
	
	$("#skickauppg").click(function() {

		var checksum = false;	
		
		var fields1 = $("#form_voices").find(".mandatory");
		var fields2 = $("#form_voices").find("input[type='radio']:checked");

		for (var i = 0; i < fields1.length; i++) {
			if ($(fields1[i]).val() == "") {
				checksum = true;
			}
		}
		
		if (fields2.length < 6) {
			checksum = true;
		}
		
		if (checksum) {
			$("#error_msg_voice").show();
			return false;
		}
	});
	
	$("body").append('<div id="tooltip_div_holder"></div>')
/*
    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'names', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
			console.log(s);
            return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0); 
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    }); 

    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'icons', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0); 
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    });
	/*
	$("#sort-table").tablesorter({
		sortList: [[0,0],[6,0],[7,0],[8,0],[9,0],[10,0]],
	   /* headers: { 
            6: { 
                sorter:'icons' 
            }, 
            7: { 
                sorter:'icons' 
            },
            8: { 
                sorter:'icons' 
            },
            9: { 
                sorter:'icons' 
            },
            10: { 
                sorter:'icons' 
            },  
        }* 
	});*/
		
	$("#sort-table").tablesorter({
		sortList: [[0,0],[6,0],[7,0],[8,0],[9,0],[10,0]]
	});	
	
	var order = 0;
	
	$('.sorticontable').live('click', function() {
		
		var sorting = null;
		
		var id = $(this).attr('id');
		switch (id) {
			case 'age-icon-sorter':
				sorting = [[6,1]];
				break;
			case 'studiowidevox-icon-sorter':
				sorting = [[7,1]];
				break;
			case 'studio-icon-sorter':
				sorting = [[8,1]];
				break;
			case 'studioconnect-icon-sorter':
				sorting = [[9,1]];
				break;
			case 'infowidevox-icon-sorter':
				sorting = [[10,1]];
				break;
			case 'name-sorter':
				if (order == 0) {
					order = 1;
				} else {
					order = 0;
				}
				sorting = [[0,order]];
				break;
		}
		
        $("#sort-table").trigger("sorton",[sorting]); 

        return false; 		
	});

	setTooltip(false);


});

function setTooltip(test) {
	
	if (test != false) {
		$("#sort-table").trigger("update"); 
	}


	
	$("#tooltip_div_holder").children().remove();
	
	$('.a_link').wTooltip(    
		{
			style: {
				border: "1px solid #CCC",
				padding: "5px",
				background: "white",
				color: "black",
				fontWeight: "bold"
			},
			className: "tooltipHover",	
		    offsetY: -30,
		    offsetX: 8,
			delay: 200,
			appendTip: "#tooltip_div_holder"	
		}
	);	
	
	$('.playbt').wTooltip(    
		{
			style: {
				border: "1px solid #CCC",
				padding: "5px",
				background: "white",
				color: "black",
				fontWeight: "bold"
			},
			className: "tooltipHover",
		    offsetY: -30,
		    offsetX: 8,
			delay: 200,
			appendTip: "#tooltip_div_holder"			
		}
	);	

	$('.a_tooltip').wTooltip(    
		{
			style: {
				border: "1px solid #CCC",
				padding: "5px",
				background: "white",
				color: "black",
				maxWidth: "220px",
				fontSize: "11px"
			},
			className: "tooltipHover",
		    offsetY: -30,
		    offsetX: 8,
			delay: 200,
			appendTip: "#tooltip_div_holder"		
		}
	);		
}


