﻿$(document).ready(function() {
	
	// IE7 100% width menu items
	if($.browser.msie && parseInt($.browser.version) == 7)
		$("#menu-items > li > ul a").each(function(){
			$(this).css("width", $(this).parents("ul").width() - 20);
		});
	
	/* input placeholders */
	
		// set placeholder
		$("input[alt]").each(function(){
			if(this.value.length==0)
				this.value = this.alt;
		})
		// focus
		.focus(function(){
			if(this.value == this.alt)
				this.value = '';
		// blur
		}).blur(function(){
			if(!this.value.length)
				this.value = this.alt;
		});
	
	/* tabs */
	var tabContents = null;
	
	$(".tab-nav li a").click(function(){
		if(!$(this).parent().hasClass("active") && !$(this).parent().hasClass("disabled") ){
			$(this).parent().siblings().removeClass("active");
			$(this).parent().addClass("active");
			
			tabContents = $(this).parent().parent("ul").siblings();
			tabContents.removeClass("active");
			tabContents.eq($(this).parent().index()).addClass("active");
			trackPageview();
		}
		
		return true;
	});
	$(".tab-nav li.disabled").css({opacity:.5});
	
	// trigger click on tab
	//trackPageview();
	//$(".tab-nav li a[href*=#"+arr[1]+"]").click();
	if (window.location.hash.length > 2)
		$(".tab-nav li a[href*=#"+window.location.hash.replace('#','')+"]").click();
	
	/* vote / rating */
	var voteStars = $("#article-vote li");
	var voted = false;
	
	if(voteStars.length){
		
		function markStars(elm){
			$(elm).addClass("active").prevAll("li").addClass("active");
		}
		
		// mouseover
		voteStars.hover(function(){
			markStars(this);
		
		// mouseout
		},function(){
			voteStars.removeClass("active");
		
		// click
		}).click(function(){
		
			if(!voted){
				markStars(this);
				
				// remove hover
				voteStars.attr("title","").unbind("mouseenter").unbind("mouseleave");
				$.post("pages/ajax/rate_article.php",{
					vote: $(this).index() + 1,
					aid: $("#aid").val()
				},function(data){
					//alert(data);
					var arr = data.split("#");
					$("#article-vote ul").remove();
					$("<br/>"+arr[0]).insertBefore("#article-vote em");
					$(".rating_status small").html(arr[1]);
				});
				
				voted = true;
				
				// blur to remove outline
				$(this).find("a").blur();
			}
			
			return false
		});
	}
	
	/* poll vote */
	var pollVote = $("#poll-vote");
	
	if(pollVote.length)
		pollVote.submit(function(){
			if ($("input[name=vote]:checked").length==0)
			{
				alert("Selecteer een antwoord");
				return false;
			}
			$.ajax({
				url: "inc/widgets/poll.php",
				type: "post",
				data: pollVote.serialize(),
				success: function(data){
					pollVote.replaceWith(data);
				},
				error: function(xhr){
					if(xhr.status == "403");
						alert("je hebt al gestemd");
				}
			});
			
			return false
		});
	
	/* print */
	$(".print").click(function(){
		window.print();
		return false;
	});
	
	/* social media */
	$(".sm-icon").click(function(){
		if(_gaq && this.rel.length);
			_gaq.push(['_trackEvent', 'Social media', 'Share', this.rel]);
	});
	
	/* widgets/oject_info toggle button */
	$("#objContactButton, #objContactButton2").click(function() {
		$("#contactForm").show(250);
		$("html, body").animate({scrollTop:0}, "slow");
		return false;
	});
	
	/* userform */
	$("input[name=profiel_type]").click(function() {
		switch(this.value)
		{
			case "private":
				$("input[name=profiel_phone_in_ad]").parent().parent().css("display","table-row");
				$("input[name=profiel_vat_num], input[name=profiel_logo]").parent().parent().css("display","none");
				break;
			case "business":
				$("input[name=profiel_phone_in_ad]").parent().parent().css("display","none");
				$("input[name=profiel_vat_num], input[name=profiel_logo]").parent().parent().css("display","table-row");
				break;
		}
	});
	
	/* seo infotext box */
	$("#info img").click(function() {
		if (!$.browser.msie)
			$("#infotext").fadeIn(250);
		else
			$("#infotext").css("display","block");
	});
	$("#info #infotext #close").click(function() {
		if (!$.browser.msie)
			$("#infotext").fadeOut(250);
		else
			$("#infotext").css("display","none");
	});
});

/**
 * "meer info" contact formulier bij object weergave
 * @return
 */
function submitContact()
{
	var p = $(".contact_form").parent();
	p.css("height",p.height());
	p.addClass('white_loader');
	
	var formdata = $(".contact_form").serialize();
	//$(".contact_form").remove();
	$(".contact_form").fadeOut(200,function(){$(this).remove();});
	
	$.post("pages/ajax/objectdetail_contact_form.php",formdata,function(data){
		$("#contactForm").html(data);
		if (data.indexOf("Verzonden") > 0)
		{
			$(".contact_form").parent().animate({height:75},250);
		}
	});
	return false;
}
