var prevTableID = 0;
var sListOpen = '';
var prevTabContent='';
var prevImgNr = 0;
var prevTabClass = '';
var prevSType = '';
$(document).ready(function(){
	showFirstSurrounding();
	$('#calendar_items tr').mouseover(function(){
		$(this).addClass("rowactive");
	}).mouseout(function(){
		$(this).removeClass("rowactive");
	});
	$('#srtbl_surrounding > li').hover(
		function()
		{
			$(this).addClass('rowactive');
		},
		function()
		{
			$(this).removeClass('rowactive');
		}
	);
	setLocMenuToolTip();
});
function setLocMenuToolTip() {
	$(".location-menu a[title]").tooltip({position:'bottom center' });
	$("#tipafriendform_1 span[title]").tooltip({position:'bottom center' });
}
function getCountry(data) {
	for (i=0;i<=300;i++) {
		$('#country_'+i).hide();
		$('#region_noimages_'+i).hide();
		$('#region_images_'+i).hide();
	}
	$('.city_wrapper').hide();
	$('.dataTables_wrapper').hide();

	if ($('#country_'+data.country).length == 0) {
		slco(data);
	}
	else {
		$('#country_'+data.country).show();
		$('#region_images_'+data.country).show();
		toggleGridMenu('grid_region_images'+data.country,true)
		sb(2);
	}
	$('#location_news').show();


}
function getRegion(data) {
	for (i=0;i<=1100;i++) {
		$('#region_noimages_'+i).hide();
		$('#region_images_'+i).hide();
//		$('#'+i+data.country+'_wrapper').hide();
	}
	$('.city_wrapper').hide();
	$('.dataTables_wrapper').hide();
	$('#location_news').hide();
	toggleGridMenu('grid_region_images'+data.country,false)

	if ($('#region_noimages_'+data.region).length == 0) {
		slre(data);
	}
	else {
		$('#region_noimages_'+data.region).show();
		$('#'+data.region+data.country+'_wrapper').show();
		toggleGridMenu('grid_country_'+data.country,false);
		sb(2);
	}
}
function getCity(data) {
	for (i=0;i<=1100;i++) {
		$('#region_noimages_'+i).hide();
		$('#region_images_'+i).hide();
//		$('#'+i+data.country+'_wrapper').hide();
	}
	$('.city_wrapper').hide();
	$('.dataTables_wrapper').hide();
	toggleGridMenu('grid_region_images'+data.country,false)

	if ($('#city_noimages_'+data.city).length == 0) {
		slci(data);
	}
	else {
		$('#city_noimages_'+data.city).show();
		$('#'+data.city+data.region+data.country+'_wrapper').show();
		toggleGridMenu('grid_country_'+data.country,false);
		sb(2);
	}
}
function getPPMenu(data) {
	$('.pp_gridmenu_wrapper').hide();
	$('.dataTables_wrapper').hide();
	if (data.pp_city == undefined)
		data.pp_city = 0;
	if ($('#category_'+data.pp_cat+data.pp_city).length == 0) {
		slppcat(data);
	}
	else {
		$('#category_'+data.pp_cat+data.pp_city).show();
//		toggleGridMenu('grid_country_'+data.country,false);
		sb(2);
	}
}
function getPPTable(data) {
	if ($('#grid_pp_category_'+data.pp_cat+data.pp_city).css('display') == 'block') 
		toggleGridMenu('grid_pp_category_'+data.pp_cat+data.pp_city)
	if ($('#grid_pp_city_'+data.pp_cat+data.pp_city).css('display') == 'block') 
		toggleGridMenu('grid_pp_city_'+data.pp_cat+data.pp_city)
	if ($('#'+data.pp_cat+data.pp_city).css('display') == 'block') 
		toggleGridMenu('grid_pp_city_'+data.pp_cat+data.pp_city)

	$('.dataTables_wrapper').hide();
	if ($('#'+data.pp+data.pp_cat+data.pp_city).length == 0) {
		gppt(data)
	}
	else {
		$('#'+data.pp+data.pp_cat+data.pp_city+'_wrapper').show();
//		toggleGridMenu('grid_country_'+data.country,false);
		sb(2);
	}
}

function starsmouseover(no,ulid) {
	for (i=0;i<=4;i++) {
		if (i  <= no)
			$('#'+ulid ).children('li:eq('+i+')').addClass("fullstar_hover");
		else 
			$('#'+ulid ).children('li:eq('+i +')').addClass("nostar_hover");
	}
}
function setmouseout() {
	$('.vote_stars_list').mouseout(function(){
		$(this).children('li').removeClass("fullstar_hover");
		$(this).children('li').removeClass("nostar_hover");
	});
}
function updateVoteStars(data) {
	voteresult = data.mm_result -1;
	voteglobalresult = data.globalresult -1;
	rv = Math.round(voteresult*100)/100;
	grv = Math.round(voteglobalresult*100)/100;
	for (i=0;i<=4;i++) {
		if (i  <= voteresult) {
			$('#'+data.cat_name ).children('li:eq('+i+')').attr('class',"fullstar");
		}
		else {
			$('#'+data.cat_name ).children('li:eq('+i+')').attr('class',"nostar");
			if (voteresult != rv && i  == Math.round(rv)) {
				$('#'+data.cat_name ).children('li:eq('+i+')').attr('class',"halfstar");
			}
		}
	}
	for (i=0;i<=4;i++) {
		if (i  <= voteglobalresult)
			$('#globalStars').children('li:eq('+i+')').attr('class',"fullstar");
		else {
			$('#globalStars' ).children('li:eq('+i+')').attr('class',"nostar"); 
			if (voteglobalresult != grv && i  == Math.round(grv)) {
				$('#globalStars' ).children('li:eq('+i+')').attr('class',"halfstar");
			}
		}
	}
	$('#'+data.cat_name+'_fips').html(data.mm_result);
	$('#'+data.cat_name+'_count').html(data.mm_count);
	$('#voteglobal_count').html(data.globalcount);
	$('#voteglobal_result').html(Math.round(data.globalresult*100)/100);

}


function getSurroundingList(data) {
	$('.srList').hide();
	$('.srHeader').hide();
	if ($('#srtbl_'+data.stype).length == 0) {
 		llssl(data);
		sListOpen = data.stype;
	}
	else if (data.stype == sListOpen) {
		$('#srtbl_'+data.stype).hide();
		$('#srtbl_'+data.stype).prev().hide();
		sListOpen = '';
	}
	else {
		$('.srtbl_'+data.stype).show();
		$('#srtbl_'+data.stype).slideDown();
		sListOpen = data.stype;
	}
}
function showSurroundingList(data,stype) {
	$('#surroundings_menu').after(data);
	$('#srtbl_'+stype).show();
}
function showSurrounding(data)
{
	var i = 0;
	$('#srtbl_surrounding > li').hide();
	$('#srtbl_surrounding > li.'+data.stype).show();
	$('#srtbl_surrounding > li.'+data.stype).each(
		function() 
		{
			$(this).removeClass('sr_list_li_odd');
			$(this).removeClass('sr_list_li_even');
			if (i % 2 == 0)
			{
				$(this).addClass('sr_list_li_even');
			}
			else
			{
				$(this).addClass('sr_list_li_odd'); 
			}
			i = i+1;
		}
	);
	$('#surroundings_menu > li').each(
		function() 
		{
			obj = $(this).find('a:first-child');
			if (obj.hasClass('srNavi_'+data.stype+'_act') == true)
			{	
				obj.toggleClass('srNavi_'+data.stype+'_act');
			}
			else
			{			
				if (obj.hasClass('srNavi_'+data.stype) == true)
					obj.toggleClass('srNavi_'+data.stype+'_act');
				if (obj.hasClass('srNavi_'+prevSType+'_act') == true)
					obj.toggleClass('srNavi_'+prevSType+'_act');
			}
		}
	);
	if (prevSType == data.stype)
	{
		prevSType  = '';
		i = 0;
		$('#sr_header').html('');
		$('#srtbl_surrounding > li').show();
		$('#srtbl_surrounding > li').each(
		function() 
		{
			$(this).removeClass('sr_list_li_odd');
			$(this).removeClass('sr_list_li_even');
			if (i % 2 == 0)
			{
				$(this).addClass('sr_list_li_even');
			}
			else
			{
				$(this).addClass('sr_list_li_odd'); 
			}
			i = i+1;
		}
	);

	}
	else
	{
		prevSType  = data.stype;
		$('#sr_header').html(prevSType);
		$('#sr_header').text(function(i, text) { return ' '+text.substr(0,1).toUpperCase()+text.substr(1);});
	}

}
function toggleGridMenu(obj,open){
	if (open == true){
		$('#'+obj).show();
		$('#'+obj).parent().children('h2').addClass('viewtogglerdown');
	}
	else if (open == false) {
		$('#'+obj).hide();
		$('#'+obj).parent().children('h2').removeClass('viewtogglerdown');
	}
	else if(!open) {

		if ($('#'+obj).css('display') == 'none') {
			$('#'+obj).show();
			$('#'+obj).parent().children('h2').addClass('viewtogglerdown')
		} 
		else {
			$('#'+obj).hide();
			$('#'+obj).parent().children('h2').removeClass('viewtogglerdown')

		}
	}
}



function cloneTable(newID){
	$("#"+newID).html('');
	$("#"+newID).html($("#dataTable").html());
	$("#"+newID).attr('class','display');
	$("#"+newID).attr('cellspacing','0');
	$("#"+newID).attr('cellpadding','0');
}

function enableRowHighlighting(newID){
	$("#"+newID+" tbody tr").hover(	function(){
			$(this).addClass("rowHighlight");
		},
		function(){
			$(this).removeClass("rowHighlight");
		}
	);
}

function slidedivlocation(actContent, rowNr){
	var actID = '#location_menu_'+actContent+'_'+rowNr
	$('.location-menu a').removeClass('actTab');
	$('#tab'+actContent).addClass('actTab');
	if(prevTabContent != actID){
		$('.locTabContent').hide();
		$(actID).slideDown(500);
		prevTabContent = actID;
	}else{
		$(actID).slideUp(10);
		$('#tab'+actContent).removeClass('actTab');
		prevTabContent = '';
	}
}
function loadLocationImg(data){
	if ($('#location_image_0'+data.no).length == 0) {
		$('#location_image_00').before('<img id="location_image_0'+data.no+'" src="clear.gif">'); 
		$('#location_image_0'+data.no).addClass('loc_images');
		$('.loc_images').hide();
		llimg(data);
	}
	else
		$('.loc_images').hide();
		$('#location_image_0'+data.no).show();
}

function showFirstSurrounding(){
	$('#surroundings_menu').next().show();
	$('#surroundings_menu').next().next().show();
/*	sListOpen = substr($('#surroundings_menu').next().next().attr('id'),6);
	if(sListOpen!="ned"){
		$(".surroundings_navi li a.srNavi_"+sListOpen).addClass('srNavi_'+sListOpen+'_act');
		prevTabClass = 'srNavi_'+sListOpen+'_act';

		$("ul.surroundings_navi li a").bind("click", function(){
			$(".surroundings_navi li a").removeClass(prevTabClass);
			prevTabClass = $(this).attr("class") + "_act";
			$(this).addClass(prevTabClass);
		});

	}
	*/
}

function changeroutePanelHeader() {
	if ($('#route_panel_header').is(':hidden') || $('#routePanel').is(':hidden')) {
		$('#route_panel_header').show();
		$('#routePanel').show();
	}
	else {
		$('#routePanel').hide();
	}
}
function setPlayer(data) {
	flowplayer("player_"+data.no, "/flowplayer/flowplayer-3.1.5.swf", { 
     
        // clip properties common to all playlist entries 
        clip: { 
            baseUrl: data.path
        }, 
        plugins: { 
            controls: { 
                playlist: true 
            } 
        } 
	}).playlist(".videoitemlist_"+data.no, {loop:true}); 
}
function showGrid() {
	sListOpen ='';
	hideElement('location_gridview');
	hideElement('location_main_view');
	$('.tx-ppnews-pi1').show();
	window.scrollTo(100, 400);
}
function substr( f_string, f_start, f_length ) {
    // Returns part of a string  
    // 
    // version: 810.1317
    // discuss at: http://phpjs.org/functions/substr
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
    f_string += '';

    if(f_start < 0) {
        f_start += f_string.length;
    }

    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }

    if(f_length < f_start) {
        f_length = f_start;
    }

    return f_string.substring(f_start, f_length);
}

