$ui(document).ready(function() {
	$ui("#main").pngFix();

	var buyvip = new Object();

	// Sliding on campaignlist on loginpage
	$ui(".buyvip-login-campaigns").each(function() {
		$ui(this).prepend('<div class="blc-left"><a href="javascript://;" title="nach links scrollen">nach links scrollen</a></div>');
		$ui(this).append('<div class="blc-right"><a href="javascript://;" title="nach rechts scrollen">nach rechts scrollen</a></div>');
		var obj = $ui(this).find(".campaign");
		var maxlength = obj.length;
		if (maxlength <= 1) return;
		var objWidth = obj.outerWidth(true);
		var counter = 0;
		var working = false;
		var divLeft = $ui(this).children(".blc-left");
		var divRight = $ui(this).children(".blc-right");

		// Click on campaign
		$ui(this).find(".campaign").css("cursor", "pointer").bind("click", function(e) {
			// Build the div only one time
			if ($ui("#btg-tooltip").length < 1) {
				$ui("#buyvip-login-btm").append('<div id="btg-tooltip"><a href="javascript://;">schlie&szlig;en</a><p>Um zu den Kampagnen zu gelangen, loggen Sie sich bitte ein.</p></div>');
			}
			var el = $ui("#btg-tooltip");
			// Position the div
			el.css({
				"left": (e.pageX - 170),
				"top": (e.pageY - 240)
			}).show();
			// Closing after 4 seconds
			var timeout = window.setTimeout(function() {
				el.hide();
			}, 4000);
			// Close-link
			$ui("#btg-tooltip a").click(function() {
				window.clearTimeout(timeout);
				el.hide();
			});
		});

		// Automation
		var interval = window.setInterval(function() {
			slider();
		}, 3000);

		// Left/right
		divLeft.bind("click", function() {
			window.clearInterval(interval);
			slider(true);
			$ui(this).blur();
			return false;
		});
		divRight.bind("click", function() {
			window.clearInterval(interval);
			slider();
			$ui(this).blur();
			return false;
		});

		// Slider-function
		function slider(dir) {
			if (working) return;
			working = true;
			if (dir) {
				dir = "+";
				counter--;
			} else {
				dir = "-";
				counter++;
			}
			if (counter < 0) {
				// if it's the most left
				counter = 0;
				working = false;
			}  else if (counter == maxlength) {
				// if it's the most right
				counter = 0;
				obj.children(".blur").animate({"opacity" : "1.0"}, "slow", function() {obj.css({"left" : "0px"});}).animate({"opacity" : "0.6"}, "slow", function() {working = false;});
			} else {
				// everything else
				obj.animate({"left": dir + "=" + objWidth}, "slow", function() {working = false;});
			}
		}
	});

	// Mouseover-functions for Campaignlist
	$ui(".campaign a").each(function() {
		var el = $ui(this).parent(".campaign").children(".hover.camp-" + $ui(this).attr("class"));
		$ui(this).bind("mouseenter", function() {
			el.removeClass("off");
		});
		$ui(this).bind("mouseout", function() {
			el.addClass("off");
		});
	});

	// Handle form label of inviteuser-tool in nav-sub
	handleFormLabel("inviteuser", "E-Mail Adresse", "", "#000");

	// Handle form label of login
	$ui("#buyvip-login .input-wrapper label").each(function() {
		var label = $ui(this);
		var input = label.next("input");
		if (input.val() != "") label.hide();
		label.addClass("label").bind("click", function() {
			label.hide();
			input.focus();
		});
		input.bind("focus", function() {
			label.hide();
		}).bind("blur", function() {
			if (input.val() == "") label.show();
		});
	});

	// Clicks on productlist
	$ui("#grid .product .unit").each(function() {
		// this.css
		$ui(this).css({"cursor": "pointer"});
		// this.click
		$ui(this).bind("click", function() {
			window.location.href = $ui(this).children("a").attr("href"); 
		});
	});

	// Invite Friends Tool
	$ui("#invite #domains input").bind("click", function() {
		var act = $ui(this).val().split(".")[0];
		var inputFields = $ui("#input-fields");
		var domain = inputFields.children(".opt-email");
		var domainAct = inputFields.children("#" + act);

		domain.addClass("off");
		domainAct.removeClass("off");

		inputFields.show();
	});

	// Select text onfocus on #invite-friends-mail-msg
	$ui("#invite-friends-mail-msg").focus(function() {
		$ui(this).select();
	});

	handleFormLabel("email-friend-1", "E-Mail-Adresse des Empfängers", "", "#000");
	handleFormLabel("email-friend-2", "E-Mail-Adresse des Empfängers", "", "#000");
	handleFormLabel("email-friend-3", "E-Mail-Adresse des Empfängers", "", "#000");
	handleFormLabel("email-friend-4", "E-Mail-Adresse des Empfängers", "", "#000");
	handleFormLabel("email-friend-5", "E-Mail-Adresse des Empfängers", "", "#000");
});

// to use the shadowbox properly, we need to set our namespace back to its original value 
jQuery.noConflict();
Shadowbox.init({
	language: 'de-DE',
	autoDimensions: true,
	players:  ['img','iframe','html','swf']
});


function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY
	} else if (document.body.scrollHeight>document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight
	}
	var windowWidth,windowHeight;
	if (self.innerHeight) {
		if (document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth
		} else {
			windowWidth = self.innerWidth
		}
		windowHeight = self.innerHeight
	} else if (document.documentElement&&document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight
	}
	if (yScroll<windowHeight) {
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	if (xScroll<windowWidth) {
		pageWidth = xScroll;
	} else {
		pageWidth=windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function animateAGBHint() {
	var pageDimensions = getPageSize();
	$ui("body").append("<div id=\"overlay\"></div>");
	$ui("#agbhint").appendTo("body");
	$ui("#overlay").css({
		"background" : "#000",
		"left" : "0",
		"opacity" : "0",
		"position" : "absolute",
		"top" : "0",
		"z-index" : "1000",
		"width" : pageDimensions[0],
		"height" : pageDimensions[1]
	});
	var agbLeft = (pageDimensions[0] - 470) / 2;
	$ui("#agbhint").css({
		"left" : agbLeft + "px",
		"opacity" : 0,
		"top" : "200px",
		"display" : "block",
		"z-index" : "5000"
	}).show();

	$ui("#overlay").animate({"opacity": 0.6}, "slow");
	$ui("#agbhint").animate({"opacity": 1}, "slow");

}
