var play = false;
var thecontent = false;
var content = false;
var content2 = false;
var thecontentwindow = false;
var contentwindow = false;
var contentwindow2 = false;
$(document).ready(function() {
	
	window.setTimeout(function() {
		$('.skype_pnh_container').html('');
		$('.skype_pnh_print_container').removeClass('skype_pnh_print_container');
	}, 800);

	
	$.fn.supersized.options = {  
        startwidth: 1024,  
        startheight: 768,  
        minsize: .5,  
        slideshow: 0,  
        slide_interval: 20000  
    };  
	$('#supersize').supersized();  
        
	$(".harmonica").live("click", function() {
		if($(this).find("h3").hasClass("active")) {
			return false;
		}
		$(this).parent().children().each( function() {
			if($(this).hasClass("harmonica")) {
				$(this).find("h3").removeClass("active");
			} else {
				$(this).slideUp(500);
				//slideToggle(this, false); 
			}
		});
		$(this).find("h3").addClass("active");
		
		var r = $(this).attr("rel");
		setTimeout(function() {
			//slideToggle("#" + r, true); 
			$("#" + r).slideDown(500, function() {
				if($(this).find(".items").length > 0) {
					i = $(this).find(".items").html();
					$(".col2-picture .items").html("");
					$(".col2 .navi").html("");
					//$(".col2-picture .items").html(i);
					$(".col2-picture").html(i);
					initSite();
				}
				$(this).jScrollPane();
			}); 
		}, 200);
	});
	
	initSite();
	$("#banner1").scrollable({circular:true}).autoscroll({interval:9000}).navigator({navi:'#navi1'});
	$("#banner2").scrollable({circular:true}).autoscroll({interval:8000}).navigator({navi:'#navi2'});
	$("#banner3").scrollable({circular:true}).autoscroll({interval:7000}).navigator({navi:'#navi3'});
	$("#banner4").scrollable({circular:true}).autoscroll({interval:6000}).navigator({navi:'#navi4'});
	
	$(".banner").hover(function() {
		$(this).stop();
		$(this).animate({"scrollTop": 200}, 400);
	}, function () {
		$(this).stop();
		$(this).animate({"scrollTop": 0}, 400);
	});
	
	$(".homeimg").click(function() {
		nextslide();
	});
	
	$(".banner").animate({"scrollTop": 0}, 0);
	content = $("#content");
	content2 = $("#content2");
	
	$(".close-page").live("click", function() {
		content.slideUp(800);
	});
	
	$("#search-btn").click(function() {
		if($.browser.msie && $.browser.version < 8) {
			return true;
		}
		$("#search-pop-up").css("zIndex", 1000);
		$("#search-btn").addClass("active");
		slideToggle("#search-pop-up", true);
		return false;
	});
	$("#search-close").click(function() {
		$("#search-btn").removeClass("active");
		slideToggle("#search-pop-up", false);
	});
	$(".autoimage").live("click", function() {
		$(".mainlink").attr("href", $(this).attr("link"));
		$(".mainlink").attr("rel", $(this).attr("rel"));
		$(".mainlink").attr("title", $(this).attr("title"));
		$(".mainimg").attr("src", $(this).attr("afbeelding"));
		
		return false;
	});
	
	$("#menu a").click(function() {
		if(play == true) {
			return false;
		}
		$("#menu a").removeClass("active");
		$(this).addClass("active");
		$.get($(this).attr("href"), function(data) {
			if(play == false) {
				play = true;
				thecontent = $("#content", data).html();
				slideContent();
			}
		});
		return false;
	});
	$("a.slider").live("click", function() {
		if(play == true) {
			return false;
		}
		$.get($(this).attr("href"), function(data) {
			if(play == false) {
				play = true;
				thecontent = $("#content", data).html();
				slideContent();
			}
		});
		return false;
	});
	$(".carlink").live("click", function() {
		if(play == true) {
			return true;
		}
		$.get($(this).attr("href"), function(data) {
			if(play == false) {
				play = true;
				thecontent = $("#content", data).html();
				thecontentwindow = $("#content-window", data).html();
				if(thecontentwindow && $("#content-window", data).length > 0) {
					$(".options").html($(".options", data).html())
					$(".controls").html($(".controls", data).html())
				}
				swipeContent(true);
			}
		});
		return false;
	});
	$(".backlink").live("click", function() {
		if(play == true) {
			return true;
		}
		$.get($(this).attr("href"), function(data) {
			if(play == false) {
				play = true;
				thecontent = $("#content", data).html();
				thecontentwindow = $("#content-window", data).html();
				if(thecontentwindow && $("#content-window", data).length > 0) {
					$(".options").html($(".options", data).html())
					$(".controls").html($(".controls", data).html())
				}
				swipeContent(false);
			}
		});
		return false;
	});
});
function slideToggle(el, bShow) {
  var $el = $(el), height = $el.data("originalHeight"), visible = $el.is(":visible");
  
  // if the bShow isn't present, get the current visibility and reverse it
  if( arguments.length == 1 ) bShow = !visible;
  
  // if the current visiblilty is the same as the requested state, cancel
  if( bShow == visible ) return false;
  
  // get the original height
  if( !height ){
    // get original height
    height = $el.show().height();
    // update the height
    $el.data("originalHeight", height);
    // if the element was hidden, hide it again
    if( !visible ) $el.hide().css({height: 0});
  }

  // expand the knowledge (instead of slideDown/Up, use custom animation which applies fix)
  if( bShow ){
    $el.show().animate({height: height}, {duration: 500});
  } else {
    $el.animate({height: 0}, {duration: 500, complete:function (){
        $el.hide();
      }
    });
  }
}
function initSite() {
	$(".txtfirst").jScrollPane({scrollbarWidth:12});
	initlinks();
	initBanners();
	initForm();
	initNavi();
	contentwindow = $("#content-window");
	contentwindow2 = $("#content-window2");
}
function initForm() {
	if($("form.mainform").length > 0) {
		var options = { 
	        success:       showResponse  // post-submit callback 
	    }; 
	    $('.mainform').ajaxForm(options); 
	}
	if($(".select-car").length > 0) {
		var caroptions = { 
	        success:       showResponseAuto  // post-submit callback 
	    }; 
	    $('.select-car select').change(function() {
	    	$('.select-car').submit();
	    });
	    $('.select-car').submit(function() { 
	    	$('.select-car').ajaxSubmit(caroptions); 
	    	return false;
	    });
	    $('.searc-form').ajaxForm(caroptions); 
	    $('.searc-form').submit(function() {
	    	$("#search-btn").removeClass("active");
	    	slideToggle("#search-pop-up", false);
	    });
	}
}
function showResponseAuto(responseText, statusText, xhr, $form)  { 
	if(play == false) {
		play = true;
		thecontent = $("#content", responseText).html();
		slideContent();
		initForm();
	}
} 
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  { 
    out = $("#theform", responseText).html();
    $("#theform").html(out);
    initForm();
} 
function initNavi() {
	$(".navi").each(function() {
		tmpw = 0;
		$(this.children).each(function() {
			tmpw += parseInt($(this).outerWidth());
			
		});
		pw = $(this).parent("div").width();
		tmpw = (pw - tmpw) / 2;
		if(tmpw > 0) {
			$(this).css("marginLeft", tmpw);
		}
	});
}
function getmerk(w) {
	
	$.get("/index.php", {getmodel:$(w).val()}, function(data) {
		$("#select-model").html(data);
	});
}
function resetTheContentWindow() {
	contentwindow.html($("#content-window2").html());
	contentwindow.show();
	contentwindow.css("postition", "relative");
	contentwindow.css("left", "0");
	contentwindow.css("top", "0");
	contentwindow2.css("postition", "relative");
	contentwindow2.css("left", "0");
	contentwindow2.css("top", "0");
	contentwindow2.hide();
	contentwindow2.html("");
	thecontent = false;
	thecontentwindow = false;
	play = false;
	
	initSite();
}
function resetTheContent() {
	content.html($("#content2").html());
	content.show();
	content.css("postition", "relative");
	content.css("left", "0");
	content.css("top", "0");
	content2.css("postition", "relative");
	content2.css("left", "0");
	content2.css("top", "0");
	content2.hide();
	content2.html("");
	thecontent = false;
	thecontentwindow = false;
	play = false;
	
	initSite();
}

function swipeContent(side) {
	w = content.width();
	
	if(contentwindow && thecontentwindow) {
		contentwindow.css("position", "absolute");
		contentwindow2.show();
		contentwindow2.css("position", "absolute");
		contentwindow2.css("top", "0px");
		if(side) {
			contentwindow2.css("left", w + "px");
		} else {
			contentwindow2.css("left", (0-w) + "px");
		}
		contentwindow2.html(thecontentwindow);
		if(side) {
			contentwindow.animate({"left": 0-w}, 1000);
		} else {
			contentwindow.animate({"left": w}, 1000);
		}
		contentwindow2.animate({"left": 0}, 1000, function() {
			resetTheContentWindow();
		});
	} else {
		slideContent();
	}
	
}

function slideContent() {
	content.css("postition", "relative");
	content2.css("postition", "relative");
	
	content.slideUp(800);
	content2.html(thecontent);
	content2.delay(200).slideDown(800, function() {
		resetTheContent();
	});
}

function fadeContent() {
	content.css("postition", "absolute");
	content2.css("postition", "absolute");
	
	content.fadeOut(800);
	content2.html(thecontent);
	content2.delay(200).fadeIn(800, function() {
		resetTheContent();
	});
}
