$( document ).ready( function() {
	
	$( "a.group" ).fancybox( {
		'hideOnContentClick': false
	} );

	(function($) {
	   $.fn.found = function(myFunction) {
		  if(this.length) {
			 myFunction.call(this);
		  }
	   };
	})(jQuery);

	$('#google_box').found(function(){
		onLoad();
	});

	$( "a.fanybox_popup" ).click(function() { 
		open_window(780,480,$(this).attr("href"));
		return false;
	} );	
	
	$( ".farbcodes a" ).click(function() { 
		var colorCode = $( this ).attr( "href" );
		colorCode = colorCode.substring( 1, colorCode.length );
		$( "#color option[value=" + colorCode + "]" ).attr( "selected", "selected" );
		if ( $( "#size" ).length == 0 ) {
			$( "form#product" ).submit();
		}
	} );
	
	$("form#product").click (function () {
		
		var submitFlag = true;
		
		$("select option:selected").each(function () {
			if ( $(this).attr('value') == "" ) {
				submitFlag = false;
			}
		});
		
		if ( submitFlag ) {
			$(this).submit();
		}
		
		/*
		$.getJSON(
			"/_application/json.php", 
			{
				id: "",
				size: "",
				color: "",
				attribute: ""
			}, 
			function(json) {
				console.log('hello');
				console.log(json.sizes);
				console.log(json.colors);
				console.log(json.attributes);
		});
		*/			
		
	});
		
	$( "a#print" ).click(function() { 
		open_window(780,480,$(this).attr("href"));
		return false;
	} );
	
	$( "a.print" ).click(function() { 
		open_window(780,480,$(this).attr("href"));
		return false;
	} );	

	$( "div.choose p.email input" ).click(function() { 
		$( "div.choose div.email" ).show();
		$( "div.choose div.print" ).hide();
		return true;
	} );
	
	$( "div.choose p.print input" ).click(function() { 
		$( "div.choose div.print" ).show();
		$( "div.choose div.email" ).hide();
		return true;
	} );

} );

function open_window(w,h,ziel) {
	h = h - 20; var x=0, y=0, parameter="";
	if (w < screen.availWidth || h < screen.availHeight) {
		x = (screen.availWidth - w - 12) / 2;
		y = (screen.availHeight - h - 104) / 2;
		if (window.opera) y = 0; // Opera positioniert unter den Symbolleisten
		if (x<0 || y<0) { x=0; y=0; }
		else parameter = "width=" + w + ",height=" + h + ",";
	}
	parameter += "left=" + x + ",top=" + y;
	parameter += ",menubar=yes,location=yes,toolbar=no,status=no";
	parameter += ",resizable=yes,scrollbars=yes";
	var Fenster = window.open(ziel,"PopUp",parameter);
	if (Fenster) Fenster.focus();
	return !Fenster;
}