/*
    How it works 
    $(document).ready(function(){ 
      $(".jq_ad_uitklappen").adUitklappen();
    });
	 
    options:
    firstTimeOpen: true, // of alleen bij de eerste view open getoont moet worden
	OpenText: "Open", // wat de link tekst is om open te gaan
	CloseText: "Dicht", // wat de link tekst is om dicht te gaan
	AnimationSpeed: 1000, // de snelheid van de animatie
	OpenHeight: 200, // hoe hoog die moet worden bij open stand
	CloseHeight: 34 // hoe hoog die moet worden bij gesloten stand
	OpenImg: "", // als deze word ingevuld dan word openText genegeerd hier alleen een url invullen
   	CloseImg: "", // als deze word ingevuld dan word closeText genegeerd hier alleen een url invullen
   		
	html:
 	<div class="jq_ad_uitklappen" style="height:34px;"></div>
*/

var verschil;
var firstTimeLoad=true;

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

function edit_padding(sel, actie, no_animate, options){
	if(no_animate){
		switch(actie){
			case "verhoog":
				var el_hoogte = $(sel).css("margin-top");
				el_hoogte = eval(el_hoogte.replace("px",""));
				$(sel).css("margin-top",el_hoogte+verschil);
				break;
			case "verlaag":
				var el_hoogte = $(sel).css("margin-top");
				el_hoogte = eval(el_hoogte.replace("px",""));
				$(sel).css("margin-top",el_hoogte-verschil);
				break;
			case "verbreed":
				break;
			case "versmal":
				break;
		}
	}else{
		switch(actie){
			case "verhoog":
				var el_hoogte = $(sel).css("margin-top");
				el_hoogte = eval(el_hoogte.replace("px",""));
				$(sel).animate({
					"marginTop": (el_hoogte+verschil)+"px"
				}, options.AnimationSpeed, function() {
					// Animation complete.
				});
				break;
			case "verlaag":
				//console.log($(sel).css("padding-top"));
				var el_hoogte = $(sel).css("margin-top");
				el_hoogte = eval(el_hoogte.replace("px",""));
				$(sel).animate({
			    	"marginTop": (el_hoogte-verschil)+"px"
			  	}, options.AnimationSpeed, function() {
			    	// Animation complete.
			  	});
				break;
			case "verbreed":
				break;
			case "versmal":
				break;
		}
	}
};

jQuery.fn.adUitklappen = function(options){
  	var defaults = {
   		firstTimeOpen: true,
   		OpenText: "Open",
   		CloseText: "Dicht",
   		OpenImg: "",
   		CloseImg: "",
   		OpenHeight: 200,
   		OpenWidth: 0,
   		linkClass: "",
   		AnimationSpeed: 1000,
   		CloseHeight: 56,
   		CloseWidth: 0,
   		NowOpen: false,
   		once: "true",
   		show_hide_list: "show",
   		nummer: ""
  	};
  	var options = $.extend(defaults, options);
  	
  	if(typeof nummer != "undefined" && nummer > 0) {
  		nummer = nummer;
  	} else {
  		nummer = 0;
  	}
	
  	var prefix = "";
  	var no_animate = false;
  	verschil = (parseInt(options.OpenHeight)-parseInt(options.CloseHeight));
  	var edit_classes = ".container_content";
  	return $(this).each(function(){
  		var thisElement = $(this);
		
  		if(thisElement.attr('title') != ""){
		    prefix = thisElement.attr('title');
		}
		
  		if(options.nummer != ""){
  			nummer = options.nummer;
  		}
		
  		if(thisElement.hasClass('ad_'+nummer) != true){
  			thisElement.addClass('ad_'+nummer);
  		}
  		
  		if(options.once == "true"){
  			if(options.OpenWidth > 0){
  				$('.ad_'+nummer).prepend("\n<div onclick='if(stopClick(event)){ $(\".ad_"+nummer+"\").adUitklappen({once: \"false\", firstTimeOpen: \""+options.firstTimeOpen+"\",AnimationSpeed: \""+options.AnimationSpeed+"\",OpenText: \""+options.OpenText+"\",CloseText: \""+options.CloseText+"\",OpenHeight: \""+options.OpenHeight+"\", CloseHeight: \""+options.CloseHeight+"\",OpenWidth: \""+options.OpenWidth+"\", CloseWidth: \""+options.CloseWidth+"\",NowOpen: \""+options.NowOpen+"\", show_hide_list: \"hide\", nummer: \""+nummer+"\"})}' class='myAdLink myAdUitklappenClassLess"+nummer+" "+options.linkClass+"' style='cursor:pointer;' >"+(options.CloseImg != "" ? "<img src='"+options.CloseImg+"'>" : options.CloseText)+"</div>\n");
  				$('.ad_'+nummer).prepend("\n<div onclick='if(stopClick(event)){ $(\".ad_"+nummer+"\").adUitklappen({once: \"false\", firstTimeOpen: \""+options.firstTimeOpen+"\",AnimationSpeed: \""+options.AnimationSpeed+"\",OpenText: \""+options.OpenText+"\",CloseText: \""+options.CloseText+"\",OpenHeight: \""+options.OpenHeight+"\", CloseHeight: \""+options.CloseHeight+"\",OpenWidth: \""+options.OpenWidth+"\", CloseWidth: \""+options.CloseWidth+"\",NowOpen: \""+options.NowOpen+"\", show_hide_list: \"show\", nummer: \""+nummer+"\"})}' class='myAdLink myAdUitklappenClassMore"+nummer+" "+options.linkClass+"' style='cursor:pointer;'>"+(options.OpenImg != "" ? "<img src='"+options.OpenImg+"'>" : options.OpenText)+"</div>\n");		
  			} else {
  				$('.ad_'+nummer).before("\n<div onclick='if(stopClick(event)){ $(\".ad_"+nummer+"\").adUitklappen({once: \"false\", firstTimeOpen: \""+options.firstTimeOpen+"\",AnimationSpeed: \""+options.AnimationSpeed+"\",OpenText: \""+options.OpenText+"\",CloseText: \""+options.CloseText+"\",OpenHeight: \""+options.OpenHeight+"\", CloseHeight: \""+options.CloseHeight+"\",OpenWidth: \""+options.OpenWidth+"\", CloseWidth: \""+options.CloseWidth+"\",NowOpen: \""+options.NowOpen+"\", show_hide_list: \"hide\", nummer: \""+nummer+"\"})}' class='myAdLink myAdUitklappenClassLess"+nummer+" "+options.linkClass+"' style='cursor:pointer;' >"+(options.CloseImg != "" ? "<img src='"+options.CloseImg+"'>" : options.CloseText)+"</div>\n");
  				$('.ad_'+nummer).before("\n<div onclick='if(stopClick(event)){ $(\".ad_"+nummer+"\").adUitklappen({once: \"false\", firstTimeOpen: \""+options.firstTimeOpen+"\",AnimationSpeed: \""+options.AnimationSpeed+"\",OpenText: \""+options.OpenText+"\",CloseText: \""+options.CloseText+"\",OpenHeight: \""+options.OpenHeight+"\", CloseHeight: \""+options.CloseHeight+"\",OpenWidth: \""+options.OpenWidth+"\", CloseWidth: \""+options.CloseWidth+"\",NowOpen: \""+options.NowOpen+"\", show_hide_list: \"show\", nummer: \""+nummer+"\"})}' class='myAdLink myAdUitklappenClassMore"+nummer+" "+options.linkClass+"' style='cursor:pointer;'>"+(options.OpenImg != "" ? "<img src='"+options.OpenImg+"'>" : options.OpenText)+"</div>\n");		
  			}
  			
  			jQuery(".myAdUitklappenClassMore"+nummer).hide();
  			jQuery(".myAdUitklappenClassMore"+nummer).addClass('hide');
			jQuery(".myAdUitklappenClassLess"+nummer).addClass('hide');
			jQuery(".myAdUitklappenClassLess"+nummer).hide();
  		}
  		
  		if(options.firstTimeOpen == true && options.NowOpen == false){
  			if(readCookie('firstTimeopen') != 'true'){
  				createCookie('firstTimeopen','true',1);
  				no_animate = true;
  			} else {
  				options.show_hide_list = "hide";
  				no_animate = true;
  			}
  		}
  		
  		function show_hide_list(toDo){
			if(toDo == "show"){
				jQuery(".myAdUitklappenClassMore"+nummer).addClass('hide');
				jQuery(".myAdUitklappenClassMore"+nummer).hide();
				jQuery(".myAdUitklappenClassLess"+nummer).removeClass('hide');
				jQuery(".myAdUitklappenClassLess"+nummer).show();

				if(no_animate){
					if(options.OpenWidth > 0){
						thisElement.css("width",options.OpenWidth+"px");
					} else {
						thisElement.css("height",options.OpenHeight+"px");
						/*if($('.headerrr').length > 0){
							$('.headerrr').css("height",options.OpenHeight+"px");
						}*/
						edit_padding(edit_classes, "verhoog", no_animate, options);
					}
				} else {
					if(options.OpenWidth > 0){
						thisElement.animate({
					    	width: options.OpenWidth+"px"
					  	}, options.AnimationSpeed, function() {
					    	// Animation complete.
					  	});
					} else {
						thisElement.animate({
					    	height: options.OpenHeight+"px"
					  	}, options.AnimationSpeed, function() {
					    	// Animation complete.
					  	});	
					  	//alert((($('.headerrr').outerHeight()-$('.headerrr').height())+(parseInt(options.OpenHeight)-parseInt(options.CloseHeight)))+"= ("+$('.headerrr').outerHeight()+"-"+$('.headerrr').height()+") +"+options.OpenHeight+"-"+options.CloseHeight);
					  	if($('.headerrr').length > 0){
						  	$('.headerrr').animate({
						    	height: (($('.headerrr').outerHeight()-$('.headerrr').height())+verschil)+"px"
						  	}, options.AnimationSpeed, function() {
						    	// Animation complete.
						  	});	
						}
						edit_padding(edit_classes, "verhoog", no_animate, options);
					}
				}
			} else {
		    	jQuery(".myAdUitklappenClassMore"+nummer).removeClass('hide');
		    	jQuery(".myAdUitklappenClassMore"+nummer).show();
			    jQuery(".myAdUitklappenClassLess"+nummer).addClass('hide');
			    jQuery(".myAdUitklappenClassLess"+nummer).hide();
			    
			    if(no_animate){
			    	if(options.CloseWidth > 0){
			    		thisElement.css("width",options.CloseWidth+"px");
			    	} else {
			    		thisElement.css("height",options.CloseHeight+"px");
			    		//$('.headerrr').css("height",options.CloseHeight+"px");
			    		if(!firstTimeLoad){
				    		edit_padding(edit_classes, "verlaag", no_animate, options);
				    	}
			    	}
			    } else {
			    	if(options.CloseWidth > 0){
			    		thisElement.animate({
					    	width: options.CloseWidth+"px"
					  	}, options.AnimationSpeed, function() {
					    	// Animation complete.
					  	});
			    	} else {
			    		thisElement.animate({
					    	height: options.CloseHeight+"px"
					  	}, options.AnimationSpeed, function() {
					    	// Animation complete.
					  	});	
					  	if($('.headerrr').length > 0){
						  	$('.headerrr').animate({
						    	height: (($('.headerrr').outerHeight()-$('.headerrr').height())-verschil)+"px"
						  	}, options.AnimationSpeed, function() {
						    	// Animation complete.
						  	});	
						}
						if(!firstTimeLoad){
							edit_padding(edit_classes, "verlaag", no_animate, options);
						}
			    	}
			    }
			}
			firstTimeLoad=false;
		}
		show_hide_list(options.show_hide_list);
		if(options.nummer == ""){ nummer++; }
  	});
}
function stopClick(event){
	if(event.stopPropagation) event.stopPropagation();
	event.cancelBubble = true;
	if(event.preventDefault) event.preventDefault();
	return true;
}
