if(typeof jQuery != "undefined") {
    $(document).ready(function() {
        var $body = $('body');
        $body.addClass('js');
		
        /**
		* Confirm Deletion
		*/
        $('input.delete, .confirm a, a.confirm').click(function(){
            /**
			* Inputs with a class of Delete show a Confimation Dialog
			* Confirming Follows the Form Action
			*/
            if(confirm('Are you sure?')) {
                return true;
            }
            else {
                $(this).blur();
                return false;
            }
        });
		
        /**
		* New Windows
		*/
        $('#content a[rel=new],#content a[rel=external]').attr('target','_blank');
		
        /**
		* jQuery UI & UI Tabs Plugin is loaded
		* @uses ui
		* @uses tabs()
		*/
        if($.ui && $.fn.tabs) {
            /**
			* Homepage Tabs
			* Finds all the categories, adding them to a list
			* Appends the new list before all the categories
			* Adds the Tabs jQuery UI interface
			* @link http://docs.jquery.com/UI/Tabs
			*/
            var $products = $('#product-tabs');
            var $product_tabs = $('#product-tabs div.inner');
            var $product_blocks = $('> div', $product_tabs);
            var $new_ul = $('<ul class="tab-title-group"></ul>');
            var $landing_tab = $(':first', $product_blocks);
            $product_tabs.addClass('ui-tabs-container').wrapInner('<div class="ui-outer-container"></div>');
			
            $product_tabs.append('<div class="fade"></div>');
			
            /**
			* Max Height for the Tabs
			*/
            var maxHeight = $.map($product_blocks, function(el) {
                return $(el).height();
            }).sort(function(a, b) {
                return b - a;
            })[0];
            //$product_tabs.height(maxHeight+25);
			
            /**
			* Setup the Tabs List
			* Creates the Tabs Navigation
			* Manipulates the Content to work with UI Tabs syntax
			*/
            $product_blocks.each(function(){
                var $block = $(this);
                var $header = $('h2', $block);
                var block_id = $block.attr('id');
                var header_title = $header.text().replace('Personalised','');

                var $new_a = $('<a></a>').text(header_title).attr('href','#'+block_id);
                var $new_li = $('<li></li>').addClass('tab-title');
                if($block.hasClass('default')) {
                    $landing_tab = $block;
                    $new_li.addClass('ui-tabs-selected');
                }
				
                $new_a.wrapInner('<span></span>').appendTo($new_li);
                $new_li.appendTo($new_ul);
            });
            var $new_ul_container = $('<div class="ui-tabs-nav-container"></div>').append($new_ul);
			
            /**
			* Apply the UI Tabs Plugin
			*/
            $move_me = $('.ui-outer-container', $products);
            var new_panel_height = 0;
            if($product_tabs.length>0) {
                var panel_height = $products.height();
                $new_ul_container.insertBefore($product_tabs).find('> ul').tabs({
                    'select' : function(e, ui) {
                        /**
						* Fixing the Height Manually
						*/
                        new_panel_height = $(ui.panel).height() + 0;
                        if(new_panel_height>panel_height || new_panel_height<panel_height) {
                            $move_me.animate({
                                'height' : new_panel_height + 'px'
                            });
                            panel_height = new_panel_height;
                        }


                    },
                    'show' : function(ui) {},
                    'fx' : {
                        opacity: 'toggle',
                        duration: 'slow'
                    },
                    'cache' : true
                }).bind('tabsselect', function(event, ui) {
                    $(ui.tab).blur();
                });
            }
        }
		
        /**
		* Fitted Boxes Plugin is loaded
		* Finds links and applies them to the main container
		* Stylable hooks to show the whole element is clickable
		* @uses fitted()
		*/
        if($.fn.fitted) {
            $('.product_item .start-designing a').fitted({
                'container'	: '.product_item',
                'status'	: true
            });
        }
		
        /**
		* Expands the Height of Elements
		* Used with Textareas
		* @uses expand()
		*/
        if($.fn.expand) {
            $('textarea.expand').expand();
        }
		
        /**
		* Set min-height of the content to that of the container
		* So it matches the height of the Navigation
		*/
        var container_height = $('#container').height();
        var content_height = $('#content').height();
        if(container_height>content_height)	{
            $('#content').css('min-height',(container_height-470)+'px');
        }
        var options_height = $('#product-builder-options').height();
        if(content_height>options_height)	{
            $('#product-builder-options').css('min-height',(container_height-400)+'px');
        }
		
        /**
		* Scrolls Smoothly to Internal Links
		* Used with Internal Anchors
		* @uses scrollto()
		*/
        if($.fn.scrollTo) {
            $('a[href^=#].scrollto').click(function(){
                var $a = $(this);
                $.scrollTo($a.attr('href'), 750, {
                    axis	: 'xy',
                    offset	: {
                        top : -20
                    },
                    onAfter	: function(){
                        // finished!
                        $a.blur();
                    }
                });
                return false;
            });
        }
		
        /**
		* Toggle a Class for Business Orders
		*/
        var $business_form = $('#business-orders-form');
        $('ul.multiple li :checkbox, dd.checkbox :checkbox', $business_form).click(function(){
            var $checkbox = $(this);
            var $parent = $checkbox.parent();
			
            if($checkbox.is(':checked')) $parent.addClass('checked');
            else $parent.removeClass('checked');
			
            $checkbox.blur();
        }).focus(function(){
            var $checkbox = $(this);
            var $parent = $checkbox.parent();
            $('img',$parent).css('border-color','#cccccc');
        }).blur(function(){
            var $checkbox = $(this);
            var $parent = $checkbox.parent();
            $('img',$parent).css('border-color','');
        }).css({
            'position':'absolute',
            'left':'-9999px'
        });
		
        /**
		* Product Size Change
		* Hides the submit fieldset
		* Submits the Form
		*/
        $('#content form.product-size').find('select').change(function(){
            $(this).parents('form:first').submit();
            $(this).blur();
        }).end().find('fieldset.submit-fieldset').addClass('access');
		
        /**
		* Tooltips on Cart Images
		* @uses tooltip()
		*/
        if($.fn.tooltip) {
            /*
			$('#content img.cart-image').tooltip({
				track: true, 
				delay: 0, 
				showURL: false, 
				showBody: "<br>", 
				extraClass: "pretty",  
				fixPNG: true, 
				opacity: 0.95, 
				left: -120
			});
			*/
			 
            $('#content img.cart-image').tooltip({
                showBody: "_LINEEND_ ",
                showURL: false
            });
			
        }


        /**
		* FlashMessenger
		* Adds an '×' link to the message which can be clicked to dismiss the message
		*/
        var $msg = $('#page-messages');
        $msg.children('.message').each(function(){
            var $message = $(this);
            var $new_a = $('<a></a>').attr('href','#dismiss').attr('title','Dismiss').addClass('dismiss').text('×');
            $new_a.appendTo($message);
            $new_a.click(function(){
                var $a = $(this);
                $a.blur();
                //$(this).parents('p.message').fadeOut(2000);
                $message.animate({
                    'height' : 0,
                    'opacity' : 0
                }, 500, function(){
                    $(this).remove();
                });
                return false;
            });
        });


        /* Predesigns selected tab */
        try {
            if (selected_tab_id != 'undefined') {
                var $pdtabs = $('#product-tabs').tabs({
                    fx: { opacity: 'toggle'},
                    select: function(event, ui) {
                        images = ['stag','hen','retro','funny','hoodies','bestseller'];
                        img = $('#banner img:first');
                        img.fadeOut('fast', function(){
                            img.attr('class', images[ui.index])
                            .attr('src', '/img/predesign/panel/' + images[ui.index] + '.jpg');
                            }).fadeIn('slow');
                        
                    }
                    
                });
                $pdtabs.tabs('select', selected_tab_id);
            }
        } catch(e) {
            selected_tab_id = 1;
        }
        
        // Preload images
        $.preloadImages = function() {
            $.each(arguments, function(i){
                jQuery('<img />').bind('load', function(){}).attr('src', this);
            });
        };

        $.preloadImages(
                '/img/predesign/panel/stag.jpg',
                '/img/predesign/panel/hen.jpg',
                '/img/predesign/panel/retro.jpg',
                '/img/predesign/panel/funny.jpg',
                '/img/predesign/panel/hoodies.jpg',
                '/img/predesign/panel/bestseller.jpg'
            );

    });



}