(function($, w, d) {

    $(function() {
        Cufon.replace('.cufon');
        
        // w._gaq = w._gaq || [];
        // var _gaq = w._gaq,
        //     pathname = w.location.pathname,
        //     clean = function(input) {
        //         input = input.toLowerCase();
        //         input = input.replace('/','-');
        //         input = input.replace(' ','_');
        //         return escape(input);
        //     },
        //     pn = clean(pathname);
        
        // highlight basket
        // $('.basket').css('backgroundColor', 'yellow').delay(500).animate({
        //     backgroundColor: '#ebeeee'
        // }, 1000);

        $(".publicationSort select").change(function() {
          this.form.submit();
        });
       
        (function() {
          var basketlist = $('.basketlist'),
              ajax = function(id, quantity, elm) {
                $.getJSON('/services/basket_quantity', { id: id, quantity: quantity }, function(data) {
                  if(typeof elm !== 'undefined' && data !== null && typeof data.quantity !== 'undefined') {
                    elm.val(data.quantity);
                  }
                  if(data !== null && typeof data.message !== 'undefined') {
                    alert(data.message);
                  }
                });
              };
          
          basketlist.find('.remove').click(function() {
            var $this = $(this),
                id = $this.attr('id').replace('quantity_remove', '');
            $this.parents('tr:first').fadeOut(300);
            ajax(id, 0);
          });
          
          basketlist.find('.quantity').blur(function() {
            var $this = $(this),
                id = $this.attr('id').replace('quantity', ''),
                quantity = $this.val();
            ajax(id, quantity, $this);
          });
           
        })();
        
        $('.materiallist input, .basketlist input').focus(function() {
            this.select();
        });
		
		/*		function ap_stopAll(player_id)
		{
			$('.audio_flash').each(function(){
				if($(this).attr('id') != player_id)
				{
					$(this).find('object')[0].SetVariable("closePlayer", 1);
				}
				else 
				{
					$(this).find('object')[0].SetVariable("closePlayer", 0);
				}
			});
		}*/
		
		$('.audioFile').click(function(e){
			 e.preventDefault();
			 if (
			 	!$(this).next('.audio_flash').length){
					$(this).each(function() {
						audio_file = $(this).attr('href');
						audio_title = $(this).text();
						audio_id = $(this).attr('id');
						audio_width = $(this).parent().width();
						flashdiv = $('<div class="audio_flash" id="' + audio_id + '"></div>');		
						$(this).after(flashdiv);
						//$(this).after(audio_title);
						//$(this).remove();
					flashdiv.flash(
						{
							swf: 'sites/all/themes/dkr/js/player.swf',
							flashvars:
							{
								soundFile: audio_file,
								//playerID: "'" + audio_id + "'",
								quality: 'high',
								lefticon: '0xb1cc54',
								lefticonhover: '0xFFFFFF',
								righticon: '0xb1cc54',
								righticonhover: '000000',
								leftbg: '0xf6f6f6',
								rightbg: '0xf6f6f6',
								rightbghover: '0xbdbdbd',
								left: '000000',
								leftbghover: 'FFFFFF',
								volslider: '0xb1cc54',
								//bg: '0xDADADA',
								voltrack: 'FFFFFF',
								border: 'FFFFFF',
								noinfo: 'yes',
								loader: '0xb1cc54',
								track: 'DADADA',
								tracker: '0xb1cc54',
								autostart: 'yes',
								transparentpagebg: 'yes',
								wmode: 'transparent'
								//pagebg: '0xb1cc54'
							},
							width: "" + audio_width + "",
							height: 30,
							params:{
									wmode: 'transparent'
							}
							//wmode: 'transparent'
							//bgcolor: '#b1cc54'
						}
					);
			});
			 };
		}); 
		
		
		
		
        
        $('.target').live('click', function(e) {
            if (e.button === 0) {
                var $this = $(this),
                    $link = $this.parent().hasClass('themelist') ? $this.find('a:first') : $this.find('a:last');
                if ($link.length > 0) {
                    var href = $link.attr('href');
                    if (href) {          
                        if ($link.attr('target') === '_blank') {
                            window.open(href);
                        } else {
                            document.location = href;
                        }
                        return false;
                    }
                }
            }
        }).find('a').click(function(e) { e.stopPropagation(); });
        
        $('.print').click(function() {
            $(this).blur();
            window.print();
            return false;
        });
        
        $('.mFrontBox').each(function() {
            var box = $(this),
                slides = box.find('.slides li'),
                tabBox = box.find('.tabs'),
                tabs = tabBox.find('a'),
                first = slides.eq(0),
                active = 0,
                targetSlide,
                arrowPositions = [0, 77, 154, 231],
                arrowTimeout,
                time = 800,
                boxInterval = '',
                createInterval = function() {
                    clearInterval(boxInterval);
                    boxInterval = setInterval(function() { change(); }, 4000);
                },
                change = function(target) {
                    if(typeof target === 'undefined') {
                        if(active === 3) {
                            target = 0;
                        } else {
                            target = active + 1;
                        }
                    }
                    if(target !== active) {
                        targetSlide = slides.eq(target);
                        targetSlide.css('zIndex', 2);
                        targetSlide.animate({
                           opacity: 1 
                        }, time, 'easeOutCubic', function() {
                            targetSlide.css('zIndex', 1).siblings().css({
                                zIndex: 0,
                                opacity: 0
                            });
                        });
                    
                        tabs.removeClass('active');
                    
                        tabBox.stop().animate({
                            backgroundPosition: '0 '+ arrowPositions[target] +'px'
                        }, time * .7, 'easeOutCubic');
                    
                        clearTimeout(arrowTimeout);
                        arrowTimeout = setTimeout(function() { tabs.eq(target).removeClass('clicked').addClass('active'); }, time/10 + (Math.abs(active - target) * time/16));
                    
                        active = target;
                    }
                };
            
            tabs.mousedown(function() {
                var index = tabs.index(this);
                tabs.eq(index).addClass('clicked');
                change(index);
                clearInterval(boxInterval);
            }).click(function() {
                $(this).blur();
                return false;
            }).disableTextSelect();
            
            // load secondary slides and create interval (after image on first slide has loaded)
            first.imageLoad(function() {
                first.siblings().css('opacity', 0).show();
                createInterval();
            });
            
            // hover on area disables interval
            box.hover(function() {
                clearInterval(boxInterval);
            }, function() {
                createInterval();
            });
            
            // play nice when window not in focus
            $(w).blur(function() {
                clearInterval(boxInterval);
            }).focus(function() {
                createInterval();
            });
            
        });
        
        var accordion = $('.accordion');
        accordion.find('.active').removeClass('active').addClass('slide').find('div').css('display', 'block');
        accordion.find('h3').disableTextSelect().mousedown(function() {
          var $this = $(this),
              div = $this.next();
          if(div.is(':visible')) {
            div.slideUp(400, 'easeOutCubic').parent().removeClass('slide');
          } else {
            div.slideDown(400, 'easeOutCubic').parent().siblings().find('div').slideUp(400, 'easeOutCubic'); 
            div.parent().addClass('slide').siblings().removeClass('slide');
          }
        });
        
        
        // tracking
        // (function() {
        //     
        //     // footer email
        //     $('#footer .email').click(function() {
        //         // _gaq.push(['_trackEvent', 'CTA: Contact', 'Footer', pathname]);
        //         _gaq.push(['_trackPageview', '/_track/cta/contact/footer/'+ pn]);
        //     });
        //     
        //     // CTA
        //     $('.cta > li').each(function() { 
        //         var li = $(this),
        //             type = clean(li.find('h3').text()),
        //             links = li.find('a');
        //         links.click(function() {
        //             // _gaq.push(['_trackEvent', 'CTA: '+ type, $(this).text(), pathname]);
        //             _gaq.push(['_d', '/_track/cta/'+ type +'/'+ clean($(this).text()) +'/click/'+ pn]);
        //         });
        //     });
        //     
        //     $('.frontcta a').click(function() {
        //         // _gaq.push(['_trackEvent', 'CTA: Frontpage', $(this).text(), pathname]);
        //         _gaq.push(['_trackPageview', '/_track/cta/frontpage/'+ clean($(this).text()) +'/click/']);                
        //     });
        //     
        // })();
    });

})(this.jQuery, this.window, this.document);


