$(document).ready(function(){
     
    if(!$('body#specials').length) {
    	var lineAnim;
		lineAnim = new SWFObject('/includes/menu2.swf', 'lineAnim', '148', '162', '6', '#FFFFFF');
	   	lineAnim.addVariable('slogan', ' ');
		lineAnim.write('lineAnim');
    } else {
        $('#specials li.menuSpecial').addClass('active');
    }
    
    $('#checkout .yab-shop-message,#checkout .yab-empty,#checkout .history-back').css('marginLeft',390);
    
    $('#default.detail .polaroid-style:odd, #bathware .productDetail:odd').addClass('alternate');
    $('#default.detail .polaroid-style').each(function() {
        $(this).hover(function() {
            $(this).addClass('active');
        },function() {
            $(this).removeClass('active');
        });
    });
    
    
    $('#products.overview #content .textMostly,#bathware.overview #content .textMostly,#specialss.overview #content .textMostly:gt(0)').each(function() {
        var infoButton = '<p class="infoButton" style="display: none"><a href="#">info</a></p>';
        $(this).find('.excerptText').hide().parent().append(infoButton);
        
        $(this).parent().find('.infoButton').click(function() {
            $(this).css('visibility','hidden').prev().fadeIn('slow');
            return false;
        });
        
    });

    $('#products.overview #content .block:gt(0),#bathware.overview #content .block:gt(0)').each(function() {
        $(this).hover(function() {
            $(this).find('.infoButton').fadeIn('slow');
        },function() {
            $(this).find('.infoButton').fadeOut('fast');
        });
        
        // insert detail page via ajax
//         $(this).find('.imagesMostly a').click(function() {
//             var parentBlock = $(this).parent().parent().parent();
//             // hide category's listing for later reinstatement
//             parentBlock.after('<div style="display:none">' + parentBlock.html() + '</div>');
//             
//             var articleLink = $(this).attr('href');
//             
//             parentBlock.addClass('positionFix').load(articleLink,function() {
//                 // when loaded
//                 initDetailPage();
//             });
//             return false;
//         });
        $(this).find('.imagesMostly a').click(function() {
            var parentBlock = $(this).parent().parent().parent();
            
            // hide category's listing for later reinstatement
            parentBlock.after('<div class="block clear positionFix"></div>');
            
            var articleLink = $(this).attr('href');
            parentBlock.hide().next().load(articleLink,function() {
                // when loaded
                initDetailPage();
            });
            return false;
        });
        
    });
    
    
    
    
    
    // minor pages menu visibility
    if($('body#about').length) {
        if ($('#content .textMostly h3.title').text() == 'contact') {
            $('.headli:eq(1)').css('visibility','hidden');
        } else {
            $('.headli:eq(0)').css('visibility','hidden');
        }
    }
    $('#register .headli:eq(2)').css('visibility','hidden');
    
	$('#checkout p.submit').prev().find('a').click(function() {
		window.open($(this).attr('href'),'terms');
		return false;
	});
	$('#yab-checkout-table').append('<tr><td colspan="4">excludes delivery costs</td></tr><tr><td colspan="4"><a href="../specials">continue shopping</a></td></tr>');
    
    
    if ($('#products.detail').length) {
    	initDetailPage();
    }

});


function initDetailPage () {
    $('#bathware .productDetail:odd').addClass('alternate');

    $('.productInfoButton').click(function() {
        $(this).hide().parent().parent().find('.excerptText').fadeIn('slow');
        return false;
    });
/*
    $('.attachmentsDiv a').each(function() {
        var out = $(this).text().replace('Artedomus ' + $('.productCategory').text(),'');
        $(this).text(out);
    });
*/
    $('#products #content .textMostly .excerptText').hide();
    
    var imageStackIndex = 100;
    $('#products p.productDetail').each(function(i) {
        $(this).css('zIndex', 100-i).click(function() {
            imageStackIndex += 1;
            $('p.productDetail').each(function(i) {
                $(this).removeClass('alignLeft').css('zIndex', i); // zIndex kludge, but kinda works
            });
            $(this).css({
                'zIndex': imageStackIndex
            }).prevAll().andSelf().addClass('alignLeft');
            return false;
        }).hover(function() {
            $(this).addClass('active');
        },function() {
            $(this).removeClass('active');
        });
        
    });
    $('#products p.polaroid-style:gt(0)').css('marginLeft',-175);
    $('#products p.polaroid-style:eq(3)').css('marginLeft',-270);
    $('#products p.polaroid-style:first,').css('clear','both');
/*
    $('#products p.polaroid-style:even').css({'clear': 'both'});
    $('#products p.polaroid-style:odd').css({'marginLeft',0});
*/

    $('p.productDetail:first').not('.polaroid-style').addClass('alignLeft active');
    
    
    // paging links
    $('.paging .prev a,.paging .next a').click(function() {
        var parentBlock = $(this).parent().parent().parent().parent().parent();
        var categoryLink = $(this).attr('href');
       
        parentBlock.load(categoryLink,function() {
            // when loaded
            initDetailPage();
        });
        return false;
    });
    // close detail and return to normal
    $('.paging .productCategory a').click(function() {
        // reinstate original category listing and then clean up
        var parentBlock = $(this).parent().parent().parent().parent().parent();
//         parentBlock.removeClass('positionFix').html(parentBlock.next().html()).next().remove();
        parentBlock.prev().show();
        parentBlock.remove();
        
        initDetailPage();
        return false;
    });
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
