﻿var Frontend = {

    SetExternalLinks: function() {

        /*
        Purpose: checks all <a>'s in the doc, if it has a rel attribute of 'external', sets to open in new window
        usage: <a href="http://www.google.com" rel="external">Google</a>
        */

        $('a[rel=external]').click(function() { window.open(this.href); return false; });
    },

    RoundedCorners: function() {
        DD_roundies.addRule('.round-cnrs', '5px');
        DD_roundies.addRule('div.subscribe', '5px');
        DD_roundies.addRule('.top-cnrs', '5px 5px 0 0');
        DD_roundies.addRule('.bottom-cnrs', '0 0 5px 5px');
        DD_roundies.addRule('.left-cnrs', '5px 0 0 5px');
        DD_roundies.addRule('.right-cnrs', '0 5px 5px 0');
    },

    ExpandCollapse: function() {
		$('#bus-com-examples h2').after('<p class="expand-ctrl"><a href="#" class="open-all open-items">Expand all</a></p>');

        $("#bus-com-examples .collapse-listing").each(function() {
            if ($(this).hasClass('collapse-open')) {
                $(this).children(".item-holder").addClass("open");
            } else {
                $(this).children(".item-holder").addClass("closed");
            }
        });

        $(".collapse-listing .closed .item-content").hide();
        $(".collapse-listing .open .item-content").show();

        $("#bus-com-examples .collapse-listing h3").click(
			function() {
			    if ($(this).parent().hasClass("open")) {
			        $(this).siblings(".item-content").slideUp("fast");
			        $(this).parent().removeClass("open");
			        $(this).parent().addClass("closed");
			    } else {
			        $(this).siblings(".item-content").slideDown("fast");
			        $(this).parent().removeClass("closed");
			        $(this).parent().addClass("open");
			    }
			    if ($("#bus-com-examples .collapse-listing .open").length == 0) {
			        $('#bus-com-examples a.open-all').html('Expand all').addClass('open-items');
			    } else if ($("#bus-com-examples .collapse-listing .closed").length == 0) {
			        $('#bus-com-examples a.open-all').html('Collapse all').removeClass('open-items');
			    }
			    return false;
			}
		);

        $('#bus-com-examples a.open-all').click(
			function() {
			    if ($(this).hasClass("open-items")) {
			        $(this).html('Collapse all');
			        $(this).removeClass('open-items');
			        $(this).parent('p').siblings(".collapse-listing").children("div").children("h3").trigger("click");
			    } else {
			        $(this).html('Expand all');
			        $(this).addClass('open-items');
			        $(this).parent('p').siblings(".collapse-listing").children("div").children("h3").trigger("click");
			    }
			    return false;
			}
		);
    },

    HomepageQuotes: function() {
		
        $('#customer-quotes-wrapper').addClass('self-clear');
        $('#customer-quotes-wrapper li').css({ 'width': '400px', 'float': 'left' });
        $('#customer-quotes-wrapper').css({ 'width': '2000px', 'position': 'absolute' });
        
        $('#customer-quotes-wrapper blockquote').css({ 'width': '370px', 'border': 'none'});

        $('#quotes-menu li a').click(
			function() {
			    var linkID = $(this).attr('href');
			    var splitID = linkID.split("-");
			    var linkNo = splitID[1];

			    var dist = ((linkNo - 1) * 400);

			    $('#customer-quotes-wrapper').animate({ left: -dist }, 400);

			    return false;
			}
		);
    },
    
    ImgRollovers : function() {
        // find images with 'rollover' class and swap with rollover image which should have 'Hi' appended to end of filename
		$("input[type=image].rollover, a.rollover img, img.rollover").hover(function(){
				$(this).attr("src",jQuery(this).attr("src").replace(/(\.[^.]+)$/, '-hi$1'));
			},function(){
				$(this).attr("src",jQuery(this).attr("src").replace(/-hi(\.[^.]+)$/, '$1'));
		});
	},    

    IE6rollovers : function() {
		$('#ie6 .btn').hover(function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		});
    },

    Init: function() {
        this.SetExternalLinks();
        this.RoundedCorners();
        this.ExpandCollapse();
        this.HomepageQuotes();
        this.ImgRollovers();
        this.IE6rollovers();
    }
};

var Homepage = {

	ProductPanel: function() {
		$('div#product-panel div.product-pod').hover(
			function() {
				$(this).siblings('div.product-pod').fadeTo(200, 0.5); 
			},
			function() {
				$(this).siblings('div.product-pod').fadeTo(200, 1); 
			}
		);
	},

	Init: function() {
		this.ProductPanel();
	}

};


/*
 * jQuery Watermark plugin
 * Version 1.0.4 (7-DIC-2009)
 * @requires jQuery v1.2.3 or later
 *
 * Examples at: http://mario.ec/static/jq-watermark/
 * Copyright (c) 2009 Mario Estrada
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/mit-license.php
 *
 */

(function($)
{
	$.watermarker = function(){};	
	$.extend($.watermarker, {
		defaults: {
			color : '#888',
			left: 4			
		},
		setDefaults: function(settings)
		{
			$.extend( $.watermarker.defaults, settings );
		},
		checkVal: function(val, label)
		{
			if(val == '') 
				$(label).show();
			else 
				$(label).hide();
		}
	});
	
	$.fn.watermark = function(text, css_options){
		var css, elems;
		css = $.extend($.watermarker.defaults, css_options);
		 elems = this.filter('input[type=text], input[type=password], textarea');
		
		elems.each(function()
		{
			var $elem, attr_name, label_text, watermark_container, watermark_label;
			var e_margin_left, e_margin_top, pos, top, height, line_height;
			
			$elem = $(this);
			attr_name = $elem.attr('placeholder') != undefined && $elem.attr('placeholder') != '' ? 'placeholder' : 'title';
			label_text = text === undefined || text === '' ? $(this).attr(attr_name) : text;
			watermark_container = $('<span class="watermark_container"></span>');
			watermark_label = $('<span class="watermark">' + label_text + '</span>');
			
			
			// If used, remove the placeholder attribute to prevent conflicts
 			if(attr_name == 'placeholder')
				$elem.removeAttr('placeholder');
			
			watermark_container.css({
				'float': $elem.css('float'),
				'position': 'relative'
			});
			
			$elem.wrap(watermark_container);
			if(this.nodeName.toLowerCase() != 'textarea')
			{
				height = $elem.outerHeight();
				top = '50%';
				line_height = height + 'px';
			}else{
                pos = $elem.position();
				e_margin_top = $elem.css('margin-top') !== 'auto' ? parseInt($elem.css('margin-top')) : 0;
                top = pos.top + parseInt($elem.css('padding-top')) + e_margin_top + parseInt($elem.css('border-top-width'));
                line_height = $elem.css('line-height');
				height = line_height === 'normal' ? parseInt($elem.css('font-size')) : line_height;
			}
			
			e_margin_left = $elem.css('margin-left') !== 'auto' ? parseInt($elem.css('margin-left')) : '0';
			
			$.watermarker.checkVal($elem.val(), watermark_label);
			
			watermark_label.css({
            	position: 'absolute',
	            fontFamily: $elem.css('font-family'),
	            fontSize: $elem.css('font-size'),
	            color: css.color,
	            left: 0,
	            right: 0,
	            bottom: 0,
	            top: top,
	            height: height + 'px',
	            lineHeight: line_height,
	            marginTop: '-' + (height / 2) + 'px',
	            marginLeft: e_margin_left + parseInt($elem.css('padding-left'))
			}).data('jq_watermark_element', $elem);
			
			watermark_label.click(function()
			{
				$($(this).data('jq_watermark_element')).focus();
			});
			
			$elem.before(watermark_label)
			.focus(function()
			{
				$.watermarker.checkVal($(this).val(), watermark_label);
				watermark_label.animate({ opacity : 0.4}, 250);
			})
			.blur(function()
			{
				$.watermarker.checkVal($(this).val(), watermark_label);
				watermark_label.animate({ opacity : 1}, 250);
			})
			.keydown(function(e)
			{
				$(watermark_label).hide();
			});
		});
		
		return this;
	};
})(jQuery);
