// all necessary event ready scripts
jQuery(document).ready(function(){
	/* @recentPlayed + @play/stop preview :: start */
		/**
		 *	- implements play/stop on all website
		 *	- we always have a max number of 3 titles and 3 buttons to change between play/stop
		 *	- item clicked needs to have the id = specificName*itemId*itemFilename (exactly the same structure - where specificName can be unique for each module)
		 *	- independent on 'this' instance of the item, we just use itemId from regular specific id
		 **/
		jQuery('.playListTitle,.playListButton,.playListPlayedTitle,.playListPlayedButton,.playItemTitle,.playItemButton').live('click', function(event){
			event.preventDefault();
			var strTemp = jQuery(this).attr('id');
			var arrValues = strTemp.split('::');
			var itemId = arrValues[1];
			// we need to escape '.' char from jQuery selector
			var arrFilename = arrValues[2].split('.');
			var intTotalarrFilename = arrFilename.length;
			// reassemble the filename // @TODO - generate the filename already stripped
			var strFilename = '';
			for (i=0;i<intTotalarrFilename;i++){
				strFilename += arrFilename[i];
				if (i+1 != intTotalarrFilename){
					strFilename += '\\.';
				}
			}
			// we don't care what user hits as we'll always define 3 titles and 3 buttons
			// list items
			var playTitle1 = jQuery('#playListTitle\\:\\::' + itemId + '\\:\\:' + strFilename);
			var playButton1 = jQuery('#playListButton\\:\\:' + itemId + '\\:\\:' + strFilename);
			// most played list items
			var playTitle2 = jQuery('#playListPlayedTitle\\:\\:' + itemId + '\\:\\:' + strFilename);
			var playButton2 = jQuery('#playListPlayedButton\\:\\:' + itemId + '\\:\\:' + strFilename);
			// most sold list items
			var playTitle3 = jQuery('#playItemTitle\\:\\:' + itemId + '\\:\\:' + strFilename);
			var playButton3 = jQuery('#playItemButton\\:\\:' + itemId + '\\:\\:' + strFilename);
			// check title of the play button
			if (playButton1.attr("title") == 'play' || playButton3.attr("title") == 'play' || playButton1.attr("title") == 'Afspil' || playButton3.attr("title") == 'Afspil'){
				// set all buttons to play - in case one got the mute button
				jQuery('.playListButton,.playListPlayedButton,.playItemButton').each(function(){
					if (jQuery(this).children('img').attr('src') == '/images/fk_graphics/icon/white_pause.png' || jQuery(this).children('img').attr('src') == '/images/fk_graphics/icon/white_play.png'){
						jQuery(this).children('img').attr('src', '/images/fk_graphics/icon/white_play.png');
					}else{
						jQuery(this).children('img').attr('src', '/images/fk_graphics/icon/gray_play.png');
					}
				});
				// set our button to mute
				playButton1.children('img').attr('src', '/images/fk_graphics/icon/gray_pause.png');
				playButton2.children('img').attr('src', '/images/fk_graphics/icon/gray_pause.png');
				playButton3.children('img').attr('src', '/images/fk_graphics/icon/white_pause.png');
				playButton1.attr('title', 'stop');
				playButton2.attr('title', 'stop');
				playButton3.attr('title', 'stop');
				playTitle1.attr('title', 'stop');
				playTitle2.attr('title', 'stop');
				playTitle3.attr('title', 'stop');
				// make ajax only when item is not from the most recent played list
				if (arrValues[0] != 'playListPlayedTitle' && arrValues[0] != 'playListPlayedButton'){
					var moduleIdRecent = jQuery('#ajaxPlayed').val();
					if (!moduleIdRecent){
						var moduleIdRecent = jQuery('#ajaxPlayedExtra').val();
					}
					if (moduleIdRecent){
						jQuery.ajax({
							async : false,
							type: 'GET',
							url: '/services/ajaxController.php',
							data: "modId=" + moduleIdRecent + "&" + "artId=" + itemId,
							success: function(msg){
								jQuery('#ajaxWrapper_' + moduleIdRecent).html(msg + '&nbsp;');
							}
						});
					}
				}
				// play the actual item preview
				playPreviewT(arrValues[2]);
			}else{
				// set our to play
				playButton1.children('img').attr('src', '/images/fk_graphics/icon/gray_play.png');
				playButton2.children('img').attr('src', '/images/fk_graphics/icon/gray_play.png');
				playButton3.children('img').attr('src', '/images/fk_graphics/icon/white_play.png');
				playButton1.attr('title', 'play');
				playButton2.attr('title', 'play');
				playButton3.attr('title', 'play');
				playTitle1.attr('title', 'play');
				playTitle2.attr('title', 'play');
				playTitle3.attr('title', 'play');
				stopPreviewT(arrValues[2]);
			}
		});
		// play preview player function
		function playPreviewT(filename){
			if(filename == undefined || filename == "") {
				return false;
			}
			var player_html = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="150" height="18" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab"> <param name="movie" value="/flash/mp3player/mp3player.swf?file=http://media.festklaveret.dk/mp3/previews/'+filename+'" /> <param name="quality" value="high" /><param name="wmode" value="window" /> <param name="devicefont" value="true" /><param name="bgcolor" value="#ffffff" /> <embed src="/flash/mp3player/mp3player.swf?file=http://media.festklaveret.dk/mp3/previews/'+filename+'" quality="high" wmode="window" devicefont="true" bgcolor="#ffffff" width="150" height="20" name="mp3player" align="top" play="true" loop="false" quality="high" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"> <\/embed> <\/object>'; 
			var d = document.getElementById('player'); 
			d.innerHTML = player_html;
			_gaq.push(['_trackEvent', 'preview', 'play', filename]);
		}
		// stop preview player function
		function stopPreviewT(filename){
			var d = document.getElementById('player'); 
			d.innerHTML = '';
			_gaq.push(['_trackEvent', 'preview', 'stop', filename]);
		}
	/* @recentPlayed + @play/stop preview :: end */
	/* @movinColumn search :: start */
		// detect browser
		var brIE7 = false;
		if (jQuery.browser.msie && jQuery.browser.version == '7.0'){
			var brIE7 = true;
		}
		jQuery(window).scroll(function() {
			var offsetTop = jQuery(window).scrollTop();
			var total = 0;
			if (offsetTop < 287){
				total = 287 - offsetTop;
				if (brIE7){
					jQuery(".rightContentFixed").removeClass('rightMovingIE7');
				}else{
					jQuery(".rightContentFixed").removeClass('rightMoving');
				}
			}else{
				total = 0;
				if (brIE7){
					jQuery(".rightContentFixed").addClass('rightMovingIE7');
				}else{
					jQuery(".rightContentFixed").addClass('rightMoving');
				}
				jQuery(".rightContentFixed").css('top', total + 'px');
			}
		});
	/* @movinColumn search :: end */
	/* @showPopupOverlay :: start */
		checkPopup();
		function checkPopup(){
			if (jQuery('#showPopupOverlay').attr('class') == 'popup_block'){
				//Fade in the Popup and add close button
				jQuery('#showPopupOverlay').fadeIn();
				//Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
				var popMargTop = (jQuery('#showPopupOverlay').height() + 80) / 2 - 100;
				var popMargLeft = (jQuery('#showPopupOverlay').width() + 80) / 2 - 30;
				//Apply Margin to Popup
				jQuery('#showPopupOverlay').css({
					'margin-top' : -popMargTop,
					'margin-left' : -popMargLeft
				});
				//Fade in Background
				jQuery('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
				jQuery('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
			}
		}
		//Close Popups and Fade Layer
		jQuery('#closeShowPopupOverlay').live('click', function() { //When clicking on the close or fade layer...
			jQuery('#fade , .popup_block').fadeOut(function() {
				//jQuery('#fade, a.closeShowPopupOverlay').remove();  //fade them both out
			});
			return false;
		});
	/* @showPopupOverlay :: end */
	/* @cart ajax action :: start */
		jQuery('.buy, .buyPlayed').live('click', function(event){
			var d = new Date();
			event.preventDefault();
			// 1. take module id and product id && show preloader
			var arrTemp = jQuery(this).attr('id').split('_');
			var prodId = arrTemp[1];
			jQuery('#preloader').css('display', 'block');
			// 2. put item into cart - ajax
			var moduleId = jQuery('#ajaxService').val();
			jQuery.ajax({
				async : false,
				type: 'GET',
				url: '/services/ajaxController.php',
				data: "modId=" + moduleId + "&product_id=" + prodId + "&cache=" + d.getTime(),
				success: function(msg){
					// run method for add to cart event
					if (msg){
						googleAddCartEvent(msg);
					}
				}
			});
			// 3. refresh normal cart - ajax
			var moduleIdCart = jQuery('#ajaxCartBig').val();
			jQuery.ajax({
				async : false,
				type: 'GET',
				url: '/services/ajaxController.php',
				data: "modId=" + moduleIdCart + "&cache=" + d.getTime(),
				success: function(msg){
					// we hide the preloader
					jQuery('#preloader').css('display', 'none');
					// take new data
					jQuery('#ajaxWrapper_' + moduleIdCart).html(msg);
				}
			});
			// 4. refresh right column cart - ajax
			var moduleIdCartRight = jQuery('#ajaxCartSmall').val();
			jQuery.ajax({
				async : false,
				type: 'GET',
				url: '/services/ajaxController.php',
				data: "modId=" + moduleIdCartRight + "&design=1" + "&cache=" + d.getTime(),
				success: function(msg){
					jQuery('#ajaxWrapper_' + moduleIdCartRight).html(msg);
				}
			});
		})
	/* @cart ajax action :: end */
});
/* @shop14 poll :: start */
function voteAjax(voteId, votePath, value){
	jQuery.ajax({
		type: 'GET',
		url: votePath + '/services/ajaxController.php', 
		data: 'modId=' + voteId + '&' + 'answerId='+ value,
		success: function(msg){
			jQuery('#shop14_area').html(msg);
	   }
	});
}
/* @shop14 poll :: end */
/* cart add event - google */
function googleAddCartEvent(filename){
    _gaq.push(['_trackEvent', 'Cart', 'addToCart', filename]);
}
/* cart add event - google */
 /*
 * TipTip
 * Copyright 2010 Drew Wilson
 * www.drewwilson.com
 * code.drewwilson.com/entry/tiptip-jquery-plugin
 *
 * Version 1.3   -   Updated: Mar. 23, 2010
 *
 * This Plug-In will create a custom tooltip to replace the default
 * browser tooltip. It is extremely lightweight and very smart in
 * that it detects the edges of the browser window and will make sure
 * the tooltip stays within the current window size. As a result the
 * tooltip will adjust itself to be displayed above, below, to the left 
 * or to the right depending on what is necessary to stay within the
 * browser window. It is completely customizable as well via CSS.
 *
 * This TipTip jQuery plug-in is dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function($){$.fn.tipTip=function(options){var defaults={activation:"hover",keepAlive:false,maxWidth:"200px",edgeOffset:3,defaultPosition:"bottom",delay:400,fadeIn:200,fadeOut:200,attribute:"title",content:false,enter:function(){},exit:function(){}};var opts=$.extend(defaults,options);if($("#tiptip_holder").length<=0){var tiptip_holder=$('<div id="tiptip_holder" style="max-width:'+opts.maxWidth+';"></div>');var tiptip_content=$('<div id="tiptip_content"></div>');var tiptip_arrow=$('<div id="tiptip_arrow"></div>');$("body").append(tiptip_holder.html(tiptip_content).prepend(tiptip_arrow.html('<div id="tiptip_arrow_inner"></div>')))}else{var tiptip_holder=$("#tiptip_holder");var tiptip_content=$("#tiptip_content");var tiptip_arrow=$("#tiptip_arrow")}return this.each(function(){var org_elem=$(this);if(opts.content){var org_title=opts.content}else{var org_title=org_elem.attr(opts.attribute)}if(org_title!=""){if(!opts.content){org_elem.removeAttr(opts.attribute)}var timeout=false;if(opts.activation=="hover"){org_elem.hover(function(){active_tiptip()},function(){if(!opts.keepAlive){deactive_tiptip()}});if(opts.keepAlive){tiptip_holder.hover(function(){},function(){deactive_tiptip()})}}else if(opts.activation=="focus"){org_elem.focus(function(){active_tiptip()}).blur(function(){deactive_tiptip()})}else if(opts.activation=="click"){org_elem.click(function(){active_tiptip();return false}).hover(function(){},function(){if(!opts.keepAlive){deactive_tiptip()}});if(opts.keepAlive){tiptip_holder.hover(function(){},function(){deactive_tiptip()})}}function active_tiptip(){opts.enter.call(this);tiptip_content.html(org_title);tiptip_holder.hide().removeAttr("class").css("margin","0");tiptip_arrow.removeAttr("style");var top=parseInt(org_elem.offset()['top']);var left=parseInt(org_elem.offset()['left']);var org_width=parseInt(org_elem.outerWidth());var org_height=parseInt(org_elem.outerHeight());var tip_w=tiptip_holder.outerWidth();var tip_h=tiptip_holder.outerHeight();var w_compare=Math.round((org_width-tip_w)/2);var h_compare=Math.round((org_height-tip_h)/2);var marg_left=Math.round(left+w_compare);var marg_top=Math.round(top+org_height+opts.edgeOffset);var t_class="";var arrow_top="";var arrow_left=Math.round(tip_w-12)/2;if(opts.defaultPosition=="bottom"){t_class="_bottom"}else if(opts.defaultPosition=="top"){t_class="_top"}else if(opts.defaultPosition=="left"){t_class="_left"}else if(opts.defaultPosition=="right"){t_class="_right"}var right_compare=(w_compare+left)<parseInt($(window).scrollLeft());var left_compare=(tip_w+left)>parseInt($(window).width());if((right_compare&&w_compare<0)||(t_class=="_right"&&!left_compare)||(t_class=="_left"&&left<(tip_w+opts.edgeOffset+5))){t_class="_right";arrow_top=Math.round(tip_h-13)/2;arrow_left=-12;marg_left=Math.round(left+org_width+opts.edgeOffset);marg_top=Math.round(top+h_compare)}else if((left_compare&&w_compare<0)||(t_class=="_left"&&!right_compare)){t_class="_left";arrow_top=Math.round(tip_h-13)/2;arrow_left=Math.round(tip_w);marg_left=Math.round(left-(tip_w+opts.edgeOffset+5));marg_top=Math.round(top+h_compare)}var top_compare=(top+org_height+opts.edgeOffset+tip_h+8)>parseInt($(window).height()+$(window).scrollTop());var bottom_compare=((top+org_height)-(opts.edgeOffset+tip_h+8))<0;if(top_compare||(t_class=="_bottom"&&top_compare)||(t_class=="_top"&&!bottom_compare)){if(t_class=="_top"||t_class=="_bottom"){t_class="_top"}else{t_class=t_class+"_top"}arrow_top=tip_h;marg_top=Math.round(top-(tip_h+5+opts.edgeOffset))}else if(bottom_compare|(t_class=="_top"&&bottom_compare)||(t_class=="_bottom"&&!top_compare)){if(t_class=="_top"||t_class=="_bottom"){t_class="_bottom"}else{t_class=t_class+"_bottom"}arrow_top=-12;marg_top=Math.round(top+org_height+opts.edgeOffset)}if(t_class=="_right_top"||t_class=="_left_top"){marg_top=marg_top+5}else if(t_class=="_right_bottom"||t_class=="_left_bottom"){marg_top=marg_top-5}if(t_class=="_left_top"||t_class=="_left_bottom"){marg_left=marg_left+5}tiptip_arrow.css({"margin-left":arrow_left+"px","margin-top":arrow_top+"px"});tiptip_holder.css({"margin-left":marg_left+"px","margin-top":marg_top+"px"}).attr("class","tip"+t_class);if(timeout){clearTimeout(timeout)}timeout=setTimeout(function(){tiptip_holder.stop(true,true).fadeIn(opts.fadeIn)},opts.delay)}function deactive_tiptip(){opts.exit.call(this);if(timeout){clearTimeout(timeout)}tiptip_holder.fadeOut(opts.fadeOut)}}})}})(jQuery);
