var error, animate = new Array();
var debug = false;

$.noConflict();
jQuery(document).ready(function($) {
	var alreadyChanged = false;
	
	$(document).ajaxSuccess(function() {if ("Cufon" in window) Cufon.refresh();});
	
	$('#main-menu ul.menu').superfish({animation: {opacity:'show',height:'show'},delay:350}); 
	$("div.tabs").tabs({ fx: { opacity: 'toggle', height: 'toggle',  duration: 'slow' }});
	$("div.tour").tabs({ fx: [{opacity:'toggle', duration:'fast'},{opacity:'toggle', duration:'slow'}] });
	$("div.accordion").accordion();
	$("ul.tabs").tabs("div.panes>div"); 
	$('.ppy').popeye({'direction':'right', 'opacity':'1'});	
	$('.ppy.left').popeye({'direction':'left', 'opacity':'1'});			
	//PORTFOLIO SPECIAL
	var special_portfolio_elements = $('#portfolio-special-elements');
	if ( special_portfolio_elements ) 
	{
		var size = 0;
		var children = special_portfolio_elements.children();
		$.each(children, function(child){size = size + 277;});
		special_portfolio_elements.css('width',size);
	
	}
	//FORM VALIDATION
	var name = $('#commentform p.comment-form-author input');
	var email = $('#commentform p.comment-form-email input');
	var comment = $('#commentform p.comment-form-comment textarea');			
	if ( name != '' ) name.addClass('required');
	if ( comment != '' ) comment.addClass('required');			
	if ( email != '' ) email.addClass('required email');			
	$("#commentform").validate();
	$('#contactform').ajaxForm(function() { alert("Your message has been sent!"); $('#contactform').clearForm();}); 			
	
	
	$('.hr').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
	}); 
	$('.content-slideshow-random').nivoSlider({effect:'random',slices:6,animSpeed:500,pauseTime:3000,startSlide:0,directionNav:true,directionNavHide:true,controlNav:false,keyboardNav:false,pauseOnHover:true,manualAdvance:false, customChange: function(){}});
	$('.content-slideshow-fade').nivoSlider({effect:'fade',slices:6,animSpeed:500,pauseTime:3000,startSlide:0,directionNav:true,directionNavHide:true,controlNav:false,keyboardNav:false,pauseOnHover:true,manualAdvance:false, customChange: function(){}});			
	$('.content-slideshow-fold').nivoSlider({effect:'fold',slices:6,animSpeed:500,pauseTime:3000,startSlide:0,directionNav:true,directionNavHide:true,controlNav:false,keyboardNav:false,pauseOnHover:true,manualAdvance:false, customChange: function(){}});						
	$('.content-slideshow-sliceup').nivoSlider({effect:'sliceUp',slices:6,animSpeed:500,pauseTime:3000,startSlide:0,directionNav:true,directionNavHide:true,controlNav:false,keyboardNav:false,pauseOnHover:true,manualAdvance:false, customChange: function(){}});									
	$('.content-slideshow-slicedown').nivoSlider({effect:'sliceDown',slices:6,animSpeed:500,pauseTime:3000,startSlide:0,directionNav:true,directionNavHide:true,controlNav:false,keyboardNav:false,pauseOnHover:true,manualAdvance:false, customChange: function(){}});												
	$('.content-slideshow-sliceupdown').nivoSlider({effect:'sliceUpDown',slices:6,animSpeed:500,pauseTime:3000,startSlide:0,directionNav:true,directionNavHide:true,controlNav:false,keyboardNav:false,pauseOnHover:true,manualAdvance:false, customChange: function(){}});															

    $(".printbutton").live('click', function() { window.print(); });

	
    $(".hider").next().hide();
    $(".hider").live({
		click: function(e) {
			if (e.target == this) return $(this).next().toggle(400);
		}
	});
	
	function styleCheckboxes() {
		$('input[type="checkbox"].styled').each(function() {
			if ($(this).hasClass("styled") && !$(this).next().hasClass("checkbox")) {
				alreadyChanged = true;
				
				$(this).css("visibility", "hidden");
				$('<span class="' + $(this).attr("type") + '"></span>').insertAfter(this);
				$(this).css({
					'width': $(this).next().width(),
					'height': $(this).next().height()
				});
				$(this).next().css({
					'marginLeft': -parseInt($(this).width())
				});
				$(this).next().addClass($(this).is(":checked") ? "checked" : "");		
			}
		});	
		
		$("label").live({
			click: function(e) {
				$("#" + $(this).attr("for")).each(function() {
					if (this.nodeName.toLowerCase() == "input" && $(this).hasClass("styled")) {
						e.preventDefault();
						if ($(this).is(":checked")) {
							$(this).attr("checked", false); 
							$(this).next().removeClass("checked");
						} else {
							$(this).attr("checked", true);
							$(this).next().addClass("checked");
						}
						if($(this).parent(".controller") != null && $(this).closest(".controller").length > 0 && typeof updateHash == 'function') updateHash();
					}
				});
			}
		});
		
		$(".checkbox").live({
			click: 	function() {
						var p = $(this).prev('input[type="checkbox"]');
						if (p.is(":checked")) {
							p.attr("checked", false); 
							$(this).removeClass("checked");
						} else {
							p.attr("checked", true);
							$(this).addClass("checked");
						}
						if(typeof updateHash == 'function') updateHash();
					}
		});
		
	}
	
	styleCheckboxes();
	
	$(window).bind({
		mouseover: function() {updateStyle();}
	});
	
	function updateStyle() {
		$('input[type="checkbox"].styled').each(function() {
				if ($(this).is(":checked")) {
					$(this).next().addClass("checked");
				} else {
					$(this).next().removeClass("checked");
				}	
		});	
	}
	$(window).bind("hashchange", function () {updateStyle()});
	
	
	$(".close-button").live({
		click: function() {
			$(this).closest(".closeble").fadeOut(200);
		},
		load: function() {
			$(this).html("x");
		}
	});
	
	
	
	$(document).ajaxError(function(e, xhr, settings) {
		if (debug && setSidebar) console.log("LOG", e, xhr, settings);
		message("Fehler beim Laden.", "e");
	});
	
	$(document).ajaxSuccess(function(e, xhr, settings) {
		if (debug) message("Erfolgreich geladen.", "s");
	});
	
	$(document).ajaxComplete(function(e, xhr, settings) {
		if ("Cufon" in window) Cufon.refresh();
	});
	
	$(document).ajaxStart(function(e, xhr, settings) {
		if (debug) message("Lädt...", "i", 500, 50000);
	});
	
	$(".opener").bind("DOMContentLoaded", function() {
		initOpener($(this).parent());
	});
	
	$(".gen-qr").live({
		mouseover: function(e) {
				     	var qrurl = $(this).text().match(/http(s)?:\/\/(www\.)?goo\.gl\/[A-Za-z0-9]{1,10}/i);
				     	if (qrurl != null) {
					     	if ($(document).find(".qrcode").length == 0)
						 		$('<div class="qrcode"></div>').appendTo($("body"));
						 	if (debug && "console" in window) console.log($(this).offset());
						 	var h = $(".qrcode");
							h.css("backgroundImage", "url('http://chart.apis.google.com/chart?cht=qr&chs=150x150&choe=UTF-8&chld=H&chf=bg,s,E41F1F00&chl=" + qrurl[0] + "')");
							h.css("left", $(this).offset().left+($(this).outerWidth(false)-h.outerWidth(true))/2);
							h.css("top", $(this).offset().top-h.outerHeight()-10);
							h.fadeIn(500);
					 	}
					}, 
		mouseout: function(e) {
					 	$(".qrcode").fadeOut(300, function() {$(this).remove()});
					}
	});
	

});


function message(text, type, zeit, ausblendzeit, messagebox) {
	var $ = jQuery;
	var klasse;
	var sel = messagebox || "#info-div";
	var ein = parseInt( (zeit && ausblendzeit) ? zeit : ((zeit || 3000)/2) );
	var aus = parseInt( (zeit && ausblendzeit) ? ausblendzeit : ( ein/2 ));
	 
	
	switch (type) {
		case "i":
			klasse = "info";
			break;
		case "w":
			klasse = "warning";
			break;
		case "e":
			klasse = "error";
			break;
		case "s":
			klasse = "success";
			break;
		default:
			klasse = "info";
			break;
	}
	
	if (debug && "console" in window) console.log(klasse, text, ein, aus, sel);
	
	if (animate[sel] == true) 
		$(sel).stop(true, true);
	
	animate[sel] = true;
	$(sel).fadeOut(0);
	$(sel).removeClass("success warning info error").addClass(klasse);
	$(sel).text(text).fadeIn(ein).fadeOut(aus, function() {
		animate[sel] = false;
		return true;
	});
	
}

function googlShort(that) {
	var $ = jQuery;
	if (googl[window.location] == null) {
		$.ajax({
			type: 'POST',
			url: '/wp-content/plugins/adnatives_controller_widget/googl.php',
			dataType: 'json',
			data: 'longUrl=' + escape(window.location),
			success: function(d) {
				googl[window.location] = d.id;
				googlFinished(googl[window.location], that);
			},
			error: function() {
				$(that).html("Kurze URL");
			},
			beforeSend: function() {
				$(that).html("");
				$(that).css("backgroundPosition", "center center");
			},
			complete: function() {
				$(that).css("backgroundPosition", "-50px center");
			}
		});
	} else {
		googlFinished(googl[window.location], that);
	}
}

function googlFinished(url, from) {
	var $ = jQuery;
	$(from).text(url).addClass("expand");
	$(from).unbind("click");
}
