$(function(){
    
    //$('#contact-pane').appendTo('body');
    
    //LP.setContactLink();  
    //LP.setContactForm(); 
    
    /*
    $('#contact-link').toggle(function(){
        $('#contact-pane').show('scale', {origin: ['top','right']}, 300, function(){
            $('#contact-pane form').fadeIn();
            $('#contact-pane .close').click(function(){
                $('#contact-link').trigger('click');
            });
        });
        return false;
    },
    function(){
        $('#contact-pane form').hide();
        $('#contact-pane').hide('scale', {origin: ['top','right']}, 300);
        $('#contact-pane .close').unbind('click');
        return false;
    });
    */
      
    $('#items p').toggle(function(){
       $(this).next().fadeIn();
    },
    function(){
       $(this).next().hide();
    });

    var player;
    var item = $('#items a').eq(0);
    
    //gallery();
    
    $('#items a').click(function(){
        item.removeClass('active');
        $(this).addClass('active');
        item = $(this);
        $('#media').css('background', '#000');
    });

    /*
    $('.gallery').click(function(){
        gallery();
        return false;
    });
    */
   
    function gallery() {
        var p = player;
        $('#media .info').html('');
        $('#video').hide();
        $('#video').attr('href', '');
        p = null;
        
        //$('#media').height(480);
        $('#media').animate({height: '480px'}, 600, 'linear', function() {
            $('#gallery').show();
            $('#media .info').show();
        
            $('#gallery').append($('#gallery1').html());
            $('#gallery img').each(function(){
                centerImage($(this));
            });
            $('#gallery').cycle({
                fx:     'fade',
                speed:   2000,
                timeout: 6000,
                pause:   0,
                after: function(currSlideElement, nextSlideElement, options, forwardFlag){
                    $('#media .info').html(nextSlideElement.title);
                },
                before: function(){
                    $('#media .info').html('');
                }
            });
        });
        //$('#gallery').show();
        
        
    }

    $('.video').click(function(){
        var p = player;
        var text = $('span', this).text();
        var href = this.href;
        $('#gallery').hide();
        $('#media .info').hide();
        $('#gallery').html('');
        //$('#media').height(360);
        $('#media').animate({height: '360px'}, 600, 'linear', function(){
            var dimensions = text;
            var parts = dimensions.split('-');
            var width = parts[0];
            var height = parts[1];
            $('#video').css({height: height + 'px', width: width + 'px', left: 0, top: 0});

            if (height < 360) {
                $('#video').css('top', (360 - height) / 2);
            }
            if (width < 640) {
                $('#video').css('left', (640 - width) / 2);
            }

            href = $('#video').attr('href', href);
            $('#video').show();
            p = $f('video', "/ui/flowplayer/flowplayer-3.2.2.swf", {

                clip: {
                    autoPlay: true
                },

                plugins: {
                   controls: {
                      backgroundColor: '#ffffff',
                      bufferGradient: 'none',
                      tooltipColor: '#999999',
                      sliderColor: '#1A1A1A',
                      timeColor: '#999999',
                      progressGradient: 'medium',
                      timeBgColor: '#1A1A1A',
                      volumeSliderColor: '#1A1A1A',
                      sliderGradient: 'none',
                      volumeSliderGradient: 'none',
                      durationColor: '#666666',
                      buttonColor: '#1A1A1A',
                      progressColor: '#666666',
                      borderRadius: '0',
                      bufferColor: '#1A1A1A',
                      buttonOverColor: '#666666',
                      backgroundGradient: 'low',
                      tooltipTextColor: '#ffffff',
                      height: 25,
                      autoHide: 'always',
                      opacity: 1.0
                   }
            		
            		
                }
            });
        });

        return false;
    });
    
    $('.audio').click(function(){
        var p = player;
        var text = $('span', this).text();
        var href = this.href;
        $('#gallery').hide();
        $('#media .info').hide();
        $('#gallery').html('');
        //$('#media').height(360);
        $('#media').animate({height: '360px'}, 600, 'linear', function(){
            var dimensions = text;
            var parts = dimensions.split('-');
            var width = parts[0];
            var height = parts[1];
            $('#video').css({height: height + 'px', width: width + 'px', left: 0, top: 0});

            if (height < 360) {
                $('#video').css('top', (360 - height) / 2);
            }
            if (width < 640) {
                $('#video').css('left', (640 - width) / 2);
            }

            $('#video').attr('href', href);
            $('#video').show();
            p = $f('video', "/ui/flowplayer/flowplayer-3.1.5.swf", {


                canvas: {backgroundColor: "#000", backgroundImage: '/content/images/lisa/lisa-0.jpg' },

                // play button
                /*
                play: {
                    url: '/assets/images/video-play-button.png',
                    width: 141,
                    height: 141
                },
                */

                clip: {
                    autoPlay: true,
                    autoBuffering: true
                },

                plugins: {
                   controls: {
                      backgroundColor: '#ffffff',
                      bufferGradient: 'none',
                      tooltipColor: '#999999',
                      sliderColor: '#1A1A1A',
                      timeColor: '#999999',
                      progressGradient: 'medium',
                      timeBgColor: '#1A1A1A',
                      volumeSliderColor: '#1A1A1A',
                      sliderGradient: 'none',
                      volumeSliderGradient: 'none',
                      durationColor: '#666666',
                      buttonColor: '#1A1A1A',
                      progressColor: '#666666',
                      borderRadius: '0',
                      bufferColor: '#1A1A1A',
                      buttonOverColor: '#666666',
                      backgroundGradient: 'low',
                      tooltipTextColor: '#ffffff',
                      height: 25,
                      autoHide: 'always',
                      opacity: 1.0
                   }
                }
            });
        });

        return false;
    });

    
    function centerImage(image){
        var width = image.width();
        var height = image.height();
        if (width < 640) {
            var leftMargin = (640 - width) / 2; 
        }
        if (height < 480) {
            var topMargin = (480 - height) / 2; 
        }
        image.css({'margin-top': topMargin + 'px', 'margin-left': leftMargin + 'px'});
    }

    if ($('.galleria').length) {
    
        $('ul.galleria').galleria({
                history   : true, // activates the history object for bookmarking, back-button etc.
                clickNext : true, // helper for making the image clickable
                insert    : '#main_image', // the containing selector for our main image
                onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

                        // fade in the image & caption
                        if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
                                image.css('display','none').fadeIn(1000);
                        }
                        caption.css('display','none').fadeIn(1000);

                        // fetch the thumbnail container
                        var _li = thumb.parents('li');

                        // fade out inactive thumbnail
                        _li.siblings().children('img.selected').fadeTo(500,0.3);

                        // fade in active thumbnail
                        thumb.fadeTo('fast',1).addClass('selected');

                        // add a title for the clickable image
                        image.attr('title','Next image >>');
                },
                onThumb : function(thumb) { // thumbnail effects goes here

                        // fetch the thumbnail container
                        var _li = thumb.parents('li');

                        // if thumbnail is active, fade all the way.
                        var _fadeTo = _li.is('.active') ? '1' : '0.3';

                        // fade in the thumbnail when finnished loading
                        thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

                        // hover effects
                        thumb.hover(
                                function() { thumb.fadeTo('fast',1); },
                                function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
                        )
                }
        });
        

    }

});

var LP = {
    
    form: null,
    
    formDirty: false,
    
    setContactLink: function(){
        $('#contact-link').toggle(function(){
            
            $('#contact-pane').fadeIn(300, function(){
                $('#contact-pane .content').fadeIn();
                $('#contact-pane .close').click(function(){
                    $('#contact-link').trigger('click');
                });
                if (!LP.formDirty) {
                    $('#cmessage').html("");
                }
            });
            $('#video').hide();
            return false;
        },
        function(){
            $('#contact-pane .content').hide();
            $('#contact-pane').hide();
            $('#contact-pane .close').unbind('click');
            if (!LP.formDirty) {
                $('#cmessage').show();
                $('#cform').show();
                LP.initContactForm();   
            }
            $('body').css('overflow', 'hidden');
            $('#video').show();
            return false;
        });
    },
    
    setContactForm: function(){
        LP.form = $('#cform').html();
        LP.initContactForm();
    },
    
    /**
     * contact form handler
     */
    initContactForm: function(){
        $('#contactForm').submit(function() { 
            $(this).ajaxSubmit({
                beforeSubmit: function(){
                    $('#cform').hide();
                    $('#cmessage').html("<div id='loading'></div>");
                    LP.formDirty = true;
                },
                success: function(responseText, statusText){
                    var parts = responseText.split("|");
                    $('#cmessage').html(parts[0]);
                    if (parts[1]) {
                        $('#cform').html(parts[1]);
                        $('#cform').show();
                        $('#contactForm').show();
                        LP.initContactForm();
                    } else {
                        $('#cform').html(LP.form);
                        LP.formDirty = false;
                    }
                }
            }); 
            return false; 
        }); 
    }
    
};

