$(document).ready(function() {
    // Open links in a new window if rel is set to external
    $('a[rel="external"]').click(function() { window.open($(this).attr('href')); return false; });

    // Adds a class of last to the last menu item in the primary and secondary navigation
    $('#menu li:last, ul.submenu li:last').addClass('last');
	
    //Activates Make Nice Inputs plugin
    try {
        ActivateMNI();
    } catch (e) {
        //that code works in CMS console
        DhtmlLoadScript("/scripts/jquery.mNI.js");
        ActivateMNI();
    };

    // IE6 error message
    if ($.browser.msie) {
        if ($.browser.version == "6.0") {
            $("div#header").before('<div class="container_12 ie6error"><p>It appears that you are using an out of date browser. Please upgrade your browser to get the most of this website. We recommend <a href="http://www.getfirefox.com/" title="Firefox web browser | Faster, more secure, &amp; customizable" rel="external">Firefox</a>, <a href="http://www.apple.com/au/safari/" title="Apple - Safari - Introducing Safari 4 - See the web in a whole new way" rel="external">Safari</a>, or <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx" title="Internet Explorer 8: Home page" rel="external">Internet Explorer 8</a>.</p></div>');
        }
    }

    // Product items

    $('div.lll-item').addClass('jsactive'); 		// - adds a class if JS is active which increases the right margin
    // $('div.lll-item').append('<p class="lll-item-view-details"><a href="">Click to view</a></p>');		// adds in the 'click to view' text

   $('div#products-container div:first, div#products-container div:nth-child(3n+4)').addClass('first'); // - adds a class to the first item in the row, to clear it on the left, which fixes some display issues.
    $('div#products-container div:nth-child(3n)').addClass('last').after('<div class="clear"></div>'); // - adds a class to the third item in the row, which is the last, to remove the right margin.
    $('div.lll-item p.product-short-description a').each(	// - makes the entire product item clickable and linked to href of the 'click to view' text.
	function() {
	    var loc = $(this).attr('href');
	    $(this).parents('div.lll-item').click(function() { window.location = loc; return false; });
	});
	$('div.lll-item').mouseover(function(){
		$('h5',this).css("color","#ffffff");
	}).mouseout(function(){
		$('h5',this).css("color","#696969");
	});

	try {
	    ActivateFancybox();
	} catch (e) {
	    //that code works in CMS console
	    DhtmlLoadScript("/scripts/jquery.fancybox.js");
	    ActivateFancybox();
	};
	
	// Print Care Instructions
	$('a.print-small').live('click', function() {

	    $('head').append('<style type="text/css">@media print {form div {display:none;} form div#header{display:none;} form div#fancy_outer[style] {display:block; left: 10px; top: 10px; position: absolute;margin-left: -10%;} form div#fancy_outer * {display:block;} form div#fancy_overlay { display: block; background-color: #ffffff; opacity: 1;} form div#fancy_outer {display: block; width: 100%; height: 100%; padding: 0; overflow: hidden; position: absolute; top: 0; left: 0} form div#fancy_inner {display: block; width: auto; height: 100%} form div#fancy_content {display: block; width: auto; height: 100%; top: 10px; right: 10px; bottom: 10px; left: 10px;} }</style>');

	    window.print();

	    return false;
	});

    //disable right-click
	$(document).bind("contextmenu", function(e) {
	    return false;
	});
	
});

$.expr[':'].id = function(el, i, match) {
    var x = $(el).filter("[id$=_" + match[3] + "]");
    if (x.length == 0) {
        return null;
    } else {
        return x;
    }
};

$.expr[':'].name = function(el, i, match) {
    var x = $(el).filter("[name$=$" + match[3] + "]");
    if (x.length == 0) {
        return null;
    } else {
        return x;
    }
};

function DhtmlLoadScript(url) {
    var e = document.createElement("script");
    e.src = url;
    e.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
}

function ActivateMNI() {
    $('input.mni').makeNiceInput();
}

function ActivateFancybox() {
    // Fancybox init
    $(".fancybox").fancybox({
        'hideOnContentClick': false,
        'frameWidth': 720,
        'frameHeight': 440,
        'overlayOpacity': 0.5,
        'overlayColor': '#000'
    });
}

function FbsClick() {
    var u = location.href;
    var t = document.title;
    window.open('http://www.facebook.com/sharer.php?u=' + encodeURIComponent(u) + '&t=' + encodeURIComponent(t), 'sharer', 'toolbar=0,status=0,width=626,height=436');
    return false;
}

function FancyboxOnComplete() {
    if (fancyTarget == "#product-email") {
        OnProductEmailShow();
    }
    if (fancyTarget == "#dCareIntructions") {
        var cor = $("#fancy_div");
        cor.find("div.smallicons").show();
    }
}

function FancyboxOnClose() {
    $("#ci-email-div").hide();
}

function FancyboxOnScroll() {
    if (fancyTarget == "#product-email" || fancyTarget == "#dCareIntructions") {
        PositionEmailForm();
    }
}

