 $(document).ready(function() {

	// Determine mouse position
	$().mousemove(function(e) {
		mousePositionX = e.pageX;
		mousePositionY = e.pageY;
	});


	/**
	 * Functions Class for flex modules
	 *
	 */
	flexFunctions = {
			
			productDetailsClosingTimeout : false,

			/**
			 * UPDATES OF PAGE CONTENTS TRIGGERED BY FLEX
 			 *
 			 */

			/**
			 * updates auction information in tabs
			 *
			 * @param  integer auctionId
			 * @param  integer productId
			 * @return	void
			 */
			initReloader:function(auctionId, productId) {

				// Set actual product id
				this.actualProductId = productId;

				// Only process, if active tab is product information tab
				if ($('#li_tab_tab-product').attr('class') == 'active') {
					// Display product information in tab element
			 		$('#ladeAnimation').show();
			 		$.get(
			 			$('#li_tab_tab-product a').attr('href') + '&productDetails=' + productId,
			 			{},
			 			function(data) {
				 			$('#tabContent').html(data);
				 			$('#ladeAnimation').hide();
				 		}
				 	);
				}

                // Only process, if active tab is alternative product media tab
				if ($('#li_tab_tab-weitere-ansichten').attr('class') == 'active') {
					// Display alternative product media in tab element
			 		$('#ladeAnimation').show();
			 		$.get(
			 			$('#li_tab_tab-weitere-ansichten a').attr('href') + '&productDetails=' + productId,
			 			{},
			 			function(data) {
				 			$('#tabContent').html(data);
				 			$('#ladeAnimation').hide();
				 		}
				 	);
				}
				return false;

			},

			/**
			 * Redirects to fixes schnaeppchen product action
			 *
			 * @return	void
			 */
			loadFSProduct:function(productId) {

				document.location.href = '/product/loadfsproduct';

			},

			/**
			 * Sets a savepoint to the cuurent page and redirects to the login page
			 *
			 * @return	void
			 */
			gotoLogin:function(pageLink) {
 
                $(document).ready(function() {
                    $.ajax({
                        type: "GET",
                        url: "/ajax/setsavepoint",
                        data: "pageLink="+pageLink,
                        success: function(msg){
                            // alert ('Savepont set to '+msg);
                            document.location.href = '/login';
                        }
                    });
                });
			},

			/**
			 * jump to active auction page
			 *
			 * @param  integer channelId
			 * @param  integer pageId
			 * @return	void
			 */
			showActiveAuction:function(channelId, pageId) {

                var https_base = 'https://' + document.location.host + '/';
                document.location.href = https_base + pageId;

			},

			/**
			 * shows text message in lightbox
			 *
			 * @param  string	msg
			 * @return	void
			 */
			showTextMessage:function(msg) {

				openMessageLightbox(msg, 400, 250, 100);

			},

			/**
			 * covers channel teaser with html div
			 *
			 * @param  integer channelId
			 * @return	void
			 */
			coverMe:function(channelId) {
				
				// Determine div element
				switch (channelId) {
					case 1:
						// Cover tv teaser element
						if ($('object#tvTeaser')) {
							$('object#tvTeaser').css('z-index','1');
							$('div.tvTeaser-wildCart').css('z-index','1');
							$('div#tvTeaserDummy').show();
							$('div#tvTeaserDummy').css('z-index','100');
						}
						// Cover tv player element
						if ($('object#tvPlayer')) {
							$('object#tvPlayer').css('z-index','1');
							$('div#tvPlayerDummy').show();
							$('div#tvPlayerDummy').css('z-index','100');
						}
						break;
					case 2:
						// Cover web teaser element
						if ($('object#webTeaser')) {
							$('object#webTeaser').css('z-index','1');
							$('div.webTeaser-wildCart').css('z-index','1');
							$('div#webTeaserDummy').show();
							$('div#webTeaserDummy').css('z-index','100');
						}
						// Cover web player element
						if ($('object#webPlayer')) {
							$('object#webPlayer').css('z-index','1');
							$('div#webPlayerDummy').show();
							$('div#webPlayerDummy').css('z-index','100');
						}
						break;
					case 3:
						// Cover request teaser element
						if ($('object#requestTeaser')) {
							$('object#requestTeaser').css('z-index','1');
							$('div.requestTeaser-wildCart').css('z-index','1');
							$('div#requestTeaserDummy').show();
							$('div#requestTeaserDummy').css('z-index','100');
						}
						// Cover request player element
						if ($('object#requestPlayer')) {
							$('object#requestPlayer').css('z-index','1');
							$('div#requestPlayerDummy').show();
							$('div#requestPlayerDummy').css('z-index','100');
						}
						break;
				}

			},

			/**
			 * uncovers channel teaser
			 *
			 * @param  integer channelId
			 * @return	void
			 */
			unCoverMe:function(channelId) {

				// Determine div element
				switch (channelId) {
					case 1:
						// Uncover tv teaser element
						if ($('object#tvTeaser')) {
							$('object#tvTeaser').show();
							$('div#tvTeaserDummy').hide();
						}
						// Uncover tv player element
						if ($('object#tvPlayer')) {
							$('object#tvPlayer').show();
							$('div#tvPlayerDummy').hide();
						}
						break;
					case 2:
						// Uncover web teaser element
						if ($('object#webTeaser')) {
							$('object#webTeaser').show();
							$('div#webTeaserDummy').hide();
						}
						// Uncover tv player element
						if ($('object#webPlayer')) {
							$('object#webPlayer').show();
							$('div#webPlayerDummy').hide();
						}
						break;
					case 3:
						// Uncover request teaser element
						if ($('object#requestTeaser')) {
							$('object#requestTeaser').show();
							$('div#requestTeaserDummy').hide();
						}
						// Uncover request player element
						if ($('object#requestPlayer')) {
							$('object#requestPlayer').show();
							$('div#requestPlayerDummy').hide();
						}
						break;
				}

			},

			/**
				* SHOW/ HIDE LAYERS TRIGGERED BY FLEX
	 			*
	 			*/

			/**
			 * shows shipping cost layer
			 *
			 * @param  integer	PageId
			 * @return	void
			 */
			showVKLayer:function(pageId) {

                openLightbox('/?id='+pageId, 754, 491, 100);

			},

			/**
			 * shows shipping cost layer
			 *
			 * @param  integer	PageId
			 * @return	void
			 */
			showVKSpecialLayer:function(pageId) {

                openLightbox('/?id='+pageId, 754, 491, 100);

			},
            
			/**
			 * shows buyer list layer for auction id
			 *
			 * @param  integer auctionId
			 * @param  integer channelId
			 * @return	void
			 */
			showBuyerListLayer:function(auctionId, channelId) {

				// Call lightbox functionality
                openLightbox('/service/?command=winnerlistlayer&aid=' + auctionId + '&cid=' + channelId, 450, 491, 100);

			},

			/**
			 * shows product details layer
			 *
			 * @param  integer productId
			 * @return	void
			 */
			showProductDetails:function(productId, upperLeftX, upperLeftY, domId) {
				
				// Determine left and top position for flash element
				var flashPosition = $('#' + domId).offset();
				
				// Set position for product information layer
				var leftPos = (parseInt(flashPosition.left) + upperLeftX) - 5;
				var topPos  = (parseInt(flashPosition.top)  + upperLeftY) - 130;
				
				// Call lightbox functionality
				openPreview(productId, '/product/productlayerajax?productId=' + productId + '&showPrebidOption=1&showBuyableOption=0', topPos, leftPos);

			},
			/**
			 * shows votingmessage in local lightbox
			 *
			 * @param  string	msg
			 * @return	void
			 */
			showVoteMessage:function(productId, msg, upperLeftX, upperLeftY, domId) {

                //alert(msg+" "+productId+" "+upperLeftX+" "+upperLeftY+" "+domId);

				// Determine left and top position for flash element
				var flashPosition = $('#' + domId).offset();

				// Set position for product information layer
				var leftPos = (parseInt(flashPosition.left) + upperLeftX) - 5;
				var topPos  = (parseInt(flashPosition.top)  + upperLeftY) - 130;

				// Call lightbox functionality
				openLocalMessageLightbox(msg, productId, '/product/prebidlayerajax', topPos, leftPos);
			},


			/**
			 * goes to product detail page
			 *
			 * @param  String productPageLink absolute page link
			 * @return	void
			 */
			gotoProductDetails:function(productPageLink) {

                document.location.href = productPageLink;

			},

            /**
			 * goes to prebid anchor at product detail page
			 *
			 * @param  String productPageLink absolute page link
			 * @return	void
			 */
			gotoPrebidSection:function(productPageLink) {

                //alert (productPageLink);
                document.location.href = productPageLink + '#prebid';

			},

            /**
			 * goes to comment anchor at product detail page
			 *
			 * @param  String productPageLink absolute page link
			 * @return	void
			 */
			gotoCommentSection:function(productPageLink) {

                //alert (productPageLink);
                document.location.href = productPageLink + '#comment';

			},

			/**
			 * hides product details layer
			 *
			 * @return	void
			 */
			hideProductDetails:function(productId) {
				
				flexFunctions.productDetailsClosingTimeout = window.setTimeout("flexFunctions.closeProductDetails('" + productId + "')", 1000);
				
			},
			
			/**
			 * hides product details layer
			 *
			 * @return	void
			 */
			closeProductDetails:function(productId) {
				
				$('.lb_' + productId).fadeOut(400, function() {
					$(this).remove();
				});
				
			},

			/**
			 * SUBMIT FLEX EVENTS TO PORTAL
	 		 *
	 		 */

			/**
			 * ...
			 *
			 * @return	void
			 */
			successfulBid:function(pageName, auctionId, channelId) {

                pageTracker._trackPageview(pageName);

                randomString = String(Math.random() * 100000000).substr(0,7);
                affilinetTrackingUrl = 'http://partners.webmasterplan.com/registersale.asp?site=5051&mode=ppl&ltype=' + String(channelId) + '&order=' + String(auctionId) + randomString;
                //alert (affilinetTrackingUrl);

                $('body').append('<img src="'+affilinetTrackingUrl+'" width=1 height=1>');

                var ia_tp="http://t23.intelliad.de/tc.php"; 
                var ia_v="1"; 
                var ia_vz="le"; 
                var ia_vv="EUR";
                var ia_po=""; 
                var ia_sh=""; 
                var ia_nf=""; 
                var ia_hr=""; 
                var ia_uo=""; 
                var ia_vo=""; 
                var ia_tr=""; 
                var ia_vp=""; 
                var ia_c1=""; 
                var ia_c2=""; 
                var ia_c3=""; 
                var ia_c4=""; 
                var ia_cl="7333631313236323131303";
                var ia_rand = Math.floor(Math.random()*11111139435231); 
                $('body').append('<img src="'+ia_tp+'?cl='+ia_cl+'&v='+ia_v+'&vz='+ia_vz+'&vv='+ia_vv+'&po='+ia_po+'&sh='+ia_sh+'&nf='+ia_nf+'&hr='+ia_hr+'&uo='+ia_uo+'&vo='+escape(ia_vo)+'&tr='+escape(ia_tr)+'&vp='+ia_vp+'&c1='+ia_c1+'&c2='+ia_c2+'&c3='+ia_c3+'&c4='+ia_c4+'&rand='+ia_rand+'" width="1" height="1" alt="intelliAd" />');
                
                // Add econda tracking to template
                $('body').append('<iframe src="/successful-bid-tracking?auctionId=' + auctionId + '" style="border:0px none; width:1px; height:1px;" />');
			},

			/**
			 * ...
			 *
			 * @return	void
			 */
			successfulVote:function() {

				alert("successfulVote");

			}
	};
});
