var timeO;

function createMarker(title, url){
	var data = "";
	data += "<b>"+title+"</b><br />";
	data += '<p><a href="'+url+'">Klik hier om deze categorie te openen. </a></p>';
	return data;
}

function reset_listings()
{
	/*
	$("img[src='http://vindeenreis.nl/img/star.png']").each(function(){
		var PREV = $(this).prev();
		if(PREV.attr('tagName') == 'SPAN')
			PREV.remove();
	});
	*/
	
	$('h2').each(function(){
		var parts = $(this).html().split('</span>');
		var ret = parts[0] + '</span>';
		for(p=1; p < parts.length; p++)
			ret += parts[p];
		ret += '</span>';
		$(this).html(ret);
	});
	/*
	var parts = $('h2:first').html().split('</span>');
	var ret = parts[0] + '</span>';
	for(p=1; p < parts.length; p++)
		ret += parts[p];
	ret += '</span>';
	alert(ret);
	*/
}

$.fn.getDataForCategorySelect = function(catId) {
    this.each(function(){
        var select = this;
        if (select.tagName == 'SELECT') {
            select.options.length = 1;
            $.getJSON("/getCategoryJSONById.json?catId=" + catId, null, function(data) {
                $.each(data, function(index, optionData) {
                    var option = new Option(optionData.TITLE, optionData.ID);
                        if ($.browser.msie) {
                            select.add(option);
                        } else {
                            select.add(option, null);
                        }
                });
            });
        }
    });
}

function showHideProfile(obj){
	objProfile = document.getElementById('tProfile');

	if (obj.innerText == '<?=SHOW_ADDRESS?>') {
		obj.innerText = '<?=HIDE_ADDRESS?>';
		objProfile.style.display = 'block';
	} else {
		obj.innerText = '<?=SHOW_ADDRESS?>';
		objProfile.style.display = 'none';
	}
}

function products_price_alert_validateFormEx(frm){
	if ( frm.minPrice.value == '' )
	{
		alert('Please provide min price.');
		frm.minPrice.focus();
		return false;
	}

	return true;
}

function visitor_register_validateFormEx(frm){
	if ( frm.newuserId.value == '' )
	{
		alert("Please provide user id.");
		frm.newuserId.focus();
		return false;
	}

	if ( frm.password.value == '' )
	{
		alert("Please provide password.");
		frm.password.focus();
		return false;
	}

	if ( frm.confirmPassword.value == '' )
	{
		alert("Please provide confirm password.");
		frm.confirmPassword.focus();
		return false;
	}

	if ( frm.password.value != frm.confirmPassword.value )
	{
		alert("Password and confirm password do not match.");
		frm.confirmPassword.focus();
		return false;
	}

	if ( frm.name.value == '' )
	{
		alert("Please provide name.");
		frm.name.focus();
		return false;
	}

	if ( frm.email.value == '' )
	{
		alert("Please provide email address.");
		frm.email.focus();
		return false;
	}

	return true;
}

function user_rating_validateFormEx(frm){
	if ( frm.review.value == '' )
	{
		alert('Please provide a review to continue.');
		frm.review.focus();
		return false;
	}

	return true;
}

//toon alle aanbieders knop: 
$('#showAllButton').click(function(){
	$('.hidden').attr('class', '');
	$('#showAllButton').slideUp("fast");
	return false;
});


var productIds = document.getElementById('productIds'); 
function addValue(val){
	productIds.value += val + ",";
}

function removeValue(val){
	productIds.value = productIds.value.substr(0, productIds.value.indexOf(","+val+",")+1) + productIds.value.substr(productIds.value.indexOf(","+val+",")+val.length+2);
}

function setHolidayMatch()
{
	clearTimeout(timeO);
	
	$('#flash_holder').hide();
	$('#flash_intro').css('display', 'none');
	$('#flash_intro').css('height', '1px');
	
	
	var jElm = $('#holidayMatch');
	jElm.attr('src', jElm.attr('title'));
	jElm.css('display', 'inline');
	jElm.css('height', '380px');
	
	return false;
}

function setFlashIntro()
{
	$('#holidayMatch').css('display', 'none');
	$('#holidayMatch').css('height', '1px');
	
	$('#flash_holder').show();
	$('#flash_intro').css('display', 'inline');
	$('#flash_intro').css('height', '370px');
	
	timeO = setTimeout(function(){
		setHolidayMatch();
	}, 24000);
	
	return false;
}

function setSwypr()
{
	setHolidayMatch();
}

$(document).ready(function(){
	
	if(first_visit)
		setFlashIntro();
	else
		setHolidayMatch();
	
	//alert(user_agent);
	/*
	if (user_agent == 'ie') {
		
	}
	else {
		var MM_contentVersion = 6;
		var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
		if (plugin) {
			var words = navigator.plugins["Shockwave Flash"].description.split(" ");
			for (var i = 0; i < words.length; ++i) {
				if (isNaN(parseInt(words[i]))) 
					continue;
				var MM_PluginVersion = words[i];
			}
			var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
		}
		else 
			if (navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0 &&
			(navigator.appVersion.indexOf("Win") != -1)) {
				document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
				document.write('on error resume next \n');
				document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
				document.write('</SCR' + 'IPT\> \n');
			}
		if (MM_FlashCanPlay) {
			if (first_visit) {
				setTimeout(function(){
					setHolidayMatch();
				}, 6000);
				$('#repeat_link').css('display', 'none');
			}
			else {
				setHolidayMatch();
				$('#repeat_link').css('display', '');
			}
		}
		else {
			setHolidayMatch();
			$('#repeat_link').css('display', 'none');
		}
	}
	*/
	//------------------------------------------------------------------------------
	//pictures from products page: 
	//------------------------------------------------------------------------------
	$('.pictures').hide();
	var vorige = '1';
	$('.picture_button').click(function(){
		var picture = $(this).attr("href");
		$('#picture'+vorige).fadeOut("fast", function(){
			if($('#picture'+vorige).css("display") == "none"){
				$('#picturebox'+vorige).hide();
				$('#picturebox'+picture).show();
				$('#picture'+picture).fadeIn("fast");
				vorige = picture;
			}
		});
		return false;
	});
	$('#picturebox1').show();
	
	//------------------------------------------------------------------------------
	//home page: 
	//------------------------------------------------------------------------------
    $("#selectCountry").getDataForCategorySelect(0);
    $("#selectCountry").change(function() {
        $("#selectArea").getDataForCategorySelect($("#selectCountry option:selected")[0].value);
    });
    $("#selectArea").change(function() {
        $("#selectCity").getDataForCategorySelect($("#selectArea option:selected")[0].value);
    });
    $("#categorySelectionForm").submit(function(){
        var catId = 0;
        if ($("#selectCity").val()!=0) {
            catId = $("#selectCity").val();
        } else if ($("#selectArea").val()!=0){
            catId = $("#selectArea").val();
        } else {
            catId = $("#selectCountry").val();
        }
        $("#catId").val(catId); 
        this.submit();
    }); 
	
    $.fn.getDataForCategorySelect = function(catId) {
    this.each(function(){
        var select = this;
        if (select.tagName == 'SELECT') {
            select.options.length = 1;
            $.getJSON("/getCategoryJSONById.json?catId=" + catId, null, function(data) {
                $.each(data, function(index, optionData) {
                    var option = new Option(optionData.TITLE, optionData.ID);
                        if ($.browser.msie) {
                            select.add(option);
                        } else {
                            select.add(option, null);
                        }
                    });
                });
            }
        });
    }
	
	//------------------------------------------------------------------------------
	//Filters on category/search_products page: 
	//------------------------------------------------------------------------------
    
	$("ul.expandable-list-country>li:gt(4)").hide();
	if ($("ul.expandable-list-country>li").length > 5) {
		$("ul.expandable-list-country>li.show-more").show();
	}
	$("ul.expandable-list-country>li.show-more").click(function(){
		$("ul.expandable-list-country>li").show();
		$("ul.expandable-list-country>li.show-more").hide();
		return false;
	});

	$("ul.expandable-list>li:gt(31)").hide();
    if ($("ul.expandable-list>li").length > 32) {
        $("ul.expandable-list>li.show-more").show();
    }
    $("ul.expandable-list>li.show-more").click(function(){
        $("ul.expandable-list>li").show();
        $("ul.expandable-list>li.show-more").hide();
        return false;
    });
	
	$("ul.expandable-list-area>li:gt(4)").hide();
	if ($("ul.expandable-list-area>li").length > 5) {
		$("ul.expandable-list-area>li.show-more").show();
	}
	$("ul.expandable-list-area>li.show-more").click(function(){
		$("ul.expandable-list-area>li").show();
		$(this).parents(".zoekfilter").css("height", "auto");
		$("ul.expandable-list-area>li.show-more").hide();
		return false;
	});
	
	$("ul.expandable-list-city>li:gt(4)").hide();
	if ($("ul.expandable-list-city>li").length > 5) {
		$("ul.expandable-list-city>li.show-more").show();
	}
	$("ul.expandable-list-city>li.show-more").click(function(){
		$("ul.expandable-list-city>li").show();
		$(this).parents(".zoekfilter").css("height", "auto");
		$("ul.expandable-list-city>li.show-more").hide();
		return false;
	});
	
	//------------------------------------------------------------------------------
	//tabs: 
	//------------------------------------------------------------------------------
	$('.tabs > div').hide(); // Hide all divs
	$('.tabs > div:first').show(); // Show the first div
	$('ul.tabNavigation li:first a').addClass('selected');
	$('ul.tabNavigation li:first').addClass('selected'); // Set the class of the first link to active
	
	$('ul.tabNavigation li a').click(function(){ //When any link is clicked
		$('ul.tabNavigation li a').removeClass('selected');
		$('ul.tabNavigation li').removeClass('selected'); // Remove active class from all links
		$(this).parent('li').children('a').addClass('selected'); //Set clicked link class to active
		$(this).parent('li').addClass('selected');
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		$('.tabs > div').hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
		return false;
	});
	
	$('#show_zoover_rating').click(function(){ //When any link is clicked
		$('ul.tabNavigation li a').removeClass('selected');
		$('ul.tabNavigation li').removeClass('selected'); // Remove active class from all links
		
		$('#zoover_tab_li').addClass('selected');
		$('#zoover_tab_li').children('a').addClass('selected');
		
		
		$('.tabs > div').hide();
		$('#tab-zoover-new').show();

		return false;
	});

	$('#meer').click(function(){ //When any link is clicked
		$('ul.tabNavigation li a').removeClass('selected');
		$('ul.tabNavigation li').removeClass('selected'); // Remove active class from all links
		$("#tab-knop-Bestemming").parent('li').children('a').addClass('selected'); //Set clicked link class to active
		$("#tab-knop-Bestemming").parent('li').addClass('selected');
		var currentTab = $(this).attr('href'); // Set variable currentTab to value of href attribute of clicked link
		$('.tabs > div').hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
		$('.tabs').effect("highlight", "#af3c0a", 1000);
		return false;
	});
	
	//load tab from the anchor givven in link	
	var currentUrl = document.location.toString();
	if (currentUrl.match('#')) { // the URL contains an anchor
		// click the navigation item corresponding to the anchor
		var currentTab = '#' + currentUrl.split('#')[1];
		$('.tabs > div').hide(); // Hide all divs
		$(currentTab).show(); // Show div with id equal to variable currentTab
	}
	
	if (location.hash) {
      $('[href=' + location.hash + ']').click();
  	}
	
	//------------------------------------------------------------------------------
	//init prettyphoto: 
	//------------------------------------------------------------------------------
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.35, /* Value betwee 0 and 1 */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});
	
	setTimeout(function(){
			$('.loadImageFromTitle').each(function(){
				var img = $('<img/>');
				img.attr('src', $(this).attr('title'));
				img.addClass('image');
				$(this).append(img);
			});
		}, 2000);
});

function init_zoover_iframe()
{
	var iframe_title = $('#zoover_iframe').attr('title');
	if (iframe_title.length > 0) {
		$('#zoover_iframe').attr('src', iframe_title);
		$('#zoover_iframe').attr('title', '');
	}
}

function init_youtube()
{
	$('.youtube_thumb').each(function(){
		var title = $(this).attr('title');
		if (title.length > 0) {
			$(this).attr('src', title);
			$(this).attr('title', '');
		}
	});
}

function init_flickr()
{
	$('.flickr_foto').each(function(){
		var title = $(this).attr('title');
		if (title.length > 0) {
			$(this).attr('src', title);
			$(this).attr('title', '');
		}
	});
}