var promointerval, promotimeout;

jQuery.noConflict();
jQuery(document).ready(function($){
	$('div.tx-ttaddress-pi1:first').css({display:'none'});
	$('.fontsizer').jfontsizer({
					applyTo: '.bodytext .pagecontent',
					changesmall: '2',
					changelarge: '2',
					expire: 30
	});

	if ($('#polaroid').length > 0) {
		$('#polaroid .news-single-img') 
		.before('<div class="polaroid_overlay"><img src="/fileadmin/najk/template/html/main/img/polaroid.png">') 
		.after('<div id="polaroid_nav" class="rockwell green">') 
		.cycle({ 
			fx: 'scrollLeft', 
			speed: 'fast', 
			timeout: 0, 
			pager: '#polaroid_nav' 
		});
	}

	//Cufon.replace('h1, .rockwell, .pagecontent h2', { fontFamily: 'rockwell' });
	//Cufon.replace('div.sub_navigation ul li ul li', { fontFamily: 'helveticaneuebold', hoverables:{li:true}, hover:{color:'#e84c3d'} });
	Cufon.replace('div.sub_navigation ul li ul li', { fontFamily: 'helveticaneuebold' });
	Cufon.replace('div.sub_navigation ul li', { fontFamily: 'helveticaneuebold', hoverables:{li:true}, hover:{color:'#ffffff'} });
	if($.browser.msie) {
		// Wrap div around hr to display hr on IE
		
		$('hr').wrap('<div class="iehr" />');
		$('.iehr').each(function(){
			var classes = $(this).children().attr('class');
			$(this).addClass(classes);
		});
	}

	jQuery('#tx-srfeuserregister-pi1-becomenajkmember').change(bnmHandler);
	jQuery('#tx-srfeuserregister-pi1-becomenajkmember').click(bnmHandler);

	jQuery('#sword').keyup(function(event) {
		if(event.keyCode == '13') {
			// prevent other event handlers
			event.stopPropagation();
			event.stopImmediatePropagation();
			event.preventDefault();
			jQuery('#sword').parent().submit();
		}
	});
	
	// When page loads...
    // Hide all content
    $(".imgholder").hide();

    // Activate first tab
    $("ul.menu li:first").addClass("active").show();

    // Show first tab content
    $(".imgholder:first").show();

    // On Click Event
    $("ul.menu li").hover(function() {
    	clearInterval(promointerval);
    	clearTimeout(promotimeout);
        $(".slider_text").show("fast");
        // Remove any "active" class
        $("ul.menu li").removeClass("active");
        // Add "active" class to selected tab
        $(this).addClass("active");
        // Hide all tab content
        $(".imgholder").hide();

        // Find the href attribute value to identify the active
        // tab + content
        var activeTab = $(this).find("a").attr("rel");
        $(activeTab).fadeIn(); // Fade in the active ID content

        return false;

    });
    
    $('#header_slider').mouseleave(function() {
    	console.log('mouseout');
    	promotimeout = setTimeout(function(){ promointerval = setInterval(switchPromo, 5000);}, 2500);
    });
    promointerval = setInterval(switchPromo, 5000);
});

function switchPromo() {
	var current = jQuery("ul.menu li.active");
	if(current.length == 0) {
		jQuery("ul.menu li:first").addClass("active");
		current = jQuery("ul.menu li.active");
	}
	jQuery("ul.menu li").removeClass("active");
	jQuery(".imgholder").hide();
	if(current.next().length > 0) {
		current.next().addClass('active');
	} else {
		jQuery("ul.menu li:first").addClass("active");
	}
	
	var activeTab = jQuery("ul.menu li.active").find("a").attr("rel");
	jQuery(activeTab).fadeIn();
}

var tout = 0;
var first = true;
var laststate = '';

function bnmHandler() {
	var disp = (jQuery('#tx-srfeuserregister-pi1-becomenajkmember').is(':checked')) ? 'block' : 'none';
	jQuery('#optional-register-fields').css({display:disp});
}

function stateOverHandler(state) {
	clearTimeout(tout);
	if(laststate == state)
		return;
		
	laststate = state;
	var states = {	friesland:{left:131, top: -100},
					groningen:{left:195, top: -110},
					drenthe:{left:193, top: -70},
					overijssel:{left:173, top: -15},
					gelderland:{left:141, top: 30},
					flevoland:{left:111, top: -27},
					utrecht:{left:81, top: 25},
					nholland:{left:56, top: -35},
					zholland:{left:34, top: 37},
					zeeland:{left:-18, top: 95},
					brabant:{left:85, top: 90},
					limburg:{left:141, top: 126},
					
					veehouderij:{left:80, top: -10},
					intens:{left:350, top: -17},
					pachtenro:{left:625, top: -50},
					tuinbouw:{left:71, top: 185},
					verbredelandbouw:{left:345, top: 260},
					akkerbouw:{left:540, top: 215},
					bedrijfsovername:{left:70, top: 345}};
	
	var imgpos = jQuery('#mapimg').position();
	jQuery('div.tx-ttaddress-pi1:first').css({zIndex:999});
	
	jQuery('.state-div').animate({opacity: 'hide'}, {duration:150});
	jQuery('#' + state).animate({opacity: 'show'}, {duration:150});

	if(first) {
		//First time, move the div to the disered position, add 10 pixels to the 'top' property, and animate is back to the original top value;
		jQuery('div.tx-ttaddress-pi1:first').css({opacity: 'hide', top:(imgpos.top + (states[state].top) +10), left: imgpos.left + states[state].left});
		jQuery('div.tx-ttaddress-pi1:first').animate({opacity: 'show', top:'-=10'},{duration:300});
		first = false;

	} else if(jQuery('div.tx-ttaddress-pi1:first').css('display') == 'block'){
		//Div is already visible, just move to the new position
		jQuery('div.tx-ttaddress-pi1:first').animate({top:imgpos.top + (states[state].top), left: imgpos.left + states[state].left},{duration:100});

	} else {
		//Div is not visible, move to the new position, then animate
		jQuery('div.tx-ttaddress-pi1:first').css({top:(imgpos.top + (states[state].top) +10), left: imgpos.left + states[state].left});
		jQuery('div.tx-ttaddress-pi1:first').animate({opacity: 'show', top:'-=10'},{duration:300});
		
	}
	
}

function balloonhover() {
	clearTimeout(tout);	
}

function stateOutHandler() {
	tout = setTimeout('realHide()',250);
}

function realHide() {return;
	laststate = '';
	jQuery('.state-div').animate({opacity: 'hide'}, {duration:150});
	jQuery('div.tx-ttaddress-pi1:first').animate({opacity: 'hide', top:'-=10'},{duration:300});
}
