﻿/*****
Create nice looking buttons

$(selector).button({
    speed: 100 // [Speed of animation]
});
*****/
//var $add = jQuery.noConflict();
(function ($) {
    $.fn.button = function (options) {
        var defaults = {
            speed: 100
        }
        var opt = $.extend(defaults, options);

        this.each(function () {
            var t = $(this);
			
			var txt = t.text() || t.val();
            if ($(':submit,:button', this)) {
                t = $('<a>').insertAfter(this)
                .addClass(this.className)
                .attr({'id': this.id});
				if (this.href && this.href != location.href+"#") {
					$(t, "a").attr('href', this.href);
				}
				if (this.onclick) {
					t.myclick = this.onclick;
					t.bind('click', function() {if(!$(t).hasClass("disabled")) {t.myclick()}});
				}
				$(this).remove();
            }
			t.text('').css({ cursor: 'pointer' })
            .append($('<span>')
            .text(txt).append('<i></i>'));
            t.bind('click', function () {
                if (t.hasClass('disabled'))
                    return false;
                else if (t.hasClass('drop')) {
					$(this).next('.dropdown').css("visibility","hidden");
				    $(this).next('.dropdown').toggleClass('open').slideToggle(opt.speed, function(){
						if (parseInt($(this).height()) > 600) {
							$(this).css("height","600px");
							$(this).jScrollPane();
						}
						$(this).css("visibility","visible");
					});

                    if (ie7) $('.dropdown').css('zoom', '1');

//					$(this).next('.dropdown').css("top","32px");
					

					$(".askFoss select").mouseleave(function(event) { event.stopPropagation(); });
                }
            });
			
            $('.dropdown').hover(function () {
            }, function () {
                $(this).slideUp(opt.speed);
            });
        });
    };
})(jQuery);


/*****
Mega drop

$(selector).megadrop();
*****/
(function ($) {
$.fn.megadrop = function () {
    this.each(function () {
        var t = $(this);

        t.hover(function () {
            // FOSS special: below 4 lines added to hide iframe on campaign items when menu dropdown is active
            var allIfr = document.getElementsByTagName('iframe'); 
            var ifr_source = "";
            for(var i=0;i<allIfr.length;i++){ 
                ifr_source = allIfr[i].getAttribute("src");
                if (ifr_source != "") {
                    if (ifr_source.indexOf("youtube") != -1) {
                        //allIfr[i].style.display=(allIfr[i].style.display=='none')?'block':'none'; 
            			allIfr[i].style.visibility=(allIfr[i].style.visibility=='hidden')?'visible':'hidden'; 
                    }
                }
            }
            $('.mainnav a').removeClass('hover');
            t.find('a').addClass('hover');
            var tWidth = t.outerWidth();
            if (t.hasClass('small')) {
                var pos = t.position().left;
                $('.megadrop', t).css({
                    left: pos,
                    right: 'auto',
                    minWidth: tWidth + 'px',
                    zoom: '1'
                });
				$('.mdrop-inner', t).css({
				 minWidth: tWidth - 31 +'px'
				});
                if (ie7)
                    $('.megadrop', t).css('width', 'auto');
            }
			// prevent native form select (dropdown) from closing the megadropdown
			$(".productFinder .oneCol select").mouseleave(function(event) { event.stopPropagation(); });
			
            if (ie7) {
                if (t.hasClass('small')) {
                    // Fix needed
                    $('.megadrop', t).stop(1, 1).delay(200).slideDown(100, function () {
                        var navWidth = t.width();
                        var dropWidth = t.outerWidth();
//                        doOut('nav: ' + navWidth);
//                        doOut('drop: ' + dropWidth);
                        if (navWidth > dropWidth)
						t.width(dropWidth+30);
						t.children('.mdrop-inner').width(navWidth +30 );
//                            t.children('.content').width(navWidth - 30);
							//t.children('.megadropBottom').width(dropWidth - 6);
                    });
                } else {
					  $('.megadrop', t).stop(1, 1).delay(200).show();
                }
            } else 
                $('.megadrop', t).stop(1, 1).delay(100).slideDown(200);
        }, function () {
			if (location.href.indexOf("menuopen") == -1) {
				$('.mainnav a').removeClass('hover');
				if (ie7) {
					$('.megadrop', t).stop(1, 1).hide();
				} else {
					$('.megadrop', t).stop(1, 1).slideUp(200);
				}
                // FOSS special: below 4 lines added to show iframe on campaign items when menu dropdown is closed
                var allIfr = document.getElementsByTagName('iframe'); 
                for(var i=0;i<allIfr.length;i++){ 
                    ifr_source = allIfr[i].getAttribute("src");
                    if (ifr_source != "") {
                        if (ifr_source.indexOf("youtube") != -1) {
                            //allIfr[i].style.display=(allIfr[i].style.display=='none')?'block':'none'; 
        		    	    allIfr[i].style.visibility=(allIfr[i].style.visibility=='hidden')?'visible':'hidden'; 
                        }
                    }
                }
			}
        });

        if(t.hasClass('prodFinder')){
            var anchor = t.find('.dropNav a');
            anchor.click(function () {
				var thisObj = $(this);
                var link = thisObj.attr('href');
				if(t.data('myClass')) { thisObj.closest('.mdrop').removeClass(t.data('myClass')); }
				var size = thisObj.closest('li').attr('class');
				t.data("myClass", size);
				
				thisObj.closest('.mdrop').addClass(size);
                $('.megadrop .block').hide();
                $('.megadrop .dropNav a').removeClass('active');
                $(this).addClass('active');
                $(link).show();
                return false;
				
				
            });
        }
    });
};
})(jQuery);


/*****
Create nice looking checkboxes

$(selector).checkbox();
*****/
(function ($) {
$.fn.checkbox = function () {
    this.each(function () {
        var t = $(this);
        var chkbx = t.siblings('input:checkbox').hide();
        if (chkbx.is(':checked')) t.addClass('on');
        t.click(function () { t.toggleClass('on'); });
    });
};
})(jQuery);


/*****
Focus/Blur input fields

$(selector).checkVal();
*****/
(function ($) {
$.fn.checkVal = function () {
    this.each(function () {
        var t = $(this);
        var txt = t.val();

        t.focus(function (e) {
            t.addClass('focus');
            if (t.val() == '' || t.val() == txt) {
                t.val('');
            }
        });
        t.blur(function () {
            if (t.val() == '' || t.val() == txt) {
                t.val(txt);
                t.removeClass('focus');
            }
        });
    });
};
})(jQuery);


/*****
Split ul in columns

$(selector).slidr({
    speed: 250, // [Animation speed in ms]
    scroll: 1 // [Amount of items to scroll]
});
*****/
(function ($) {
$.fn.slidr = function (options) {
    var defaults = {
        speed: 250,
        scroll: 1
    }
    var opt = $.extend(defaults, options);

    return this.each(function () {
        var t = $(this);
        var tt = t.find('.slidr');
        var ul = t.find('.slidr ul');
        var li = tt.find('li');
        var amount = li.length;
        var liWidth = li.width();
        var ulWidth = ul.width((amount * liWidth));
        var target = $('<div class="target"><a id="imgTarget" href=""><img /></a></div>').prependTo(t);
        var arrows = $('<div class="arrows" />').html('<div class="prev" /><div class="next" />');
        t.append(arrows);

        // Show image
        tt.find('li:first').addClass('active');
        $('.target img', t).attr('src', $('li:first a', t).attr('href'));
        
        // Show link
        $('#imgTarget', t).attr('href', $('li:first', t).attr('link'));
        
        li.click(function(){
            if($(this).hasClass('active')) return false;
            li.removeClass('active');
            var imgPath = $(this).addClass('active').find('a').attr('href');
            var prodLink = $(this).attr('link');
			var prodName = $(this).attr('prodname');
			var prodDesc = $(this).attr('proddesc');
            $('.target img', t).attr('src', imgPath);
            $('#imgTarget').attr('href', prodLink);
			$(this).parents(".content").children("h3:first").html(prodName);
			$(this).parents(".content").children("h4:first").html(prodDesc);
            return false;
        });

        // Slidr scroll
        $('.arrows div', t).live('click', function(){
            var arr = $(this);
            if(arr.hasClass('off') || ul.is(':animated')) return false;
            var scrollSize = (liWidth * opt.scroll);

            if(arr.hasClass('prev')) {
                if(parseInt(ul.css('left')) == 0){
                    return false;
                }
                ul.animate({
                    'left': '+=' + scrollSize + 'px'
                }, opt.speed);
            } else {
                if(parseInt(ul.css('left')) == -(amount * liWidth - (liWidth * 2))){
                    return false;
                }
                ul.animate({
                    'left': '-=' + scrollSize + 'px'
                }, opt.speed);
            }
        });

        $(document).keyup(function(e){
            var key = e.charCode || e.keyCode || 0;
            // right 39 // left  37
            switch(key){
                case 37:
                    $('.arrows .prev', t).click();
                    break;
                case 39:
                    $('.arrows .next', t).click();
                    break;
            }
        });
    });
};
})(jQuery);


/*****
Makes selectors equal height

$(selector).equalHeight();
*****/
(function ($) {
$.fn.equalHeight = function () {
    var height = 0;
    var maxHeight = 0;

    // Store the tallest element's height
    this.each(function () {
        height = $(this).outerHeight();
        maxHeight = (height > maxHeight) ? height : maxHeight;
    });

    // Set element's min-height to tallest element's height
    return this.each(function () {
        var t = $(this);
        var minHeight = maxHeight - (t.outerHeight() - t.height());
        var property = (!ie7) ? 'height' : 'min-height';
        var minHeight = (!ie7) ? (minHeight - 2) : minHeight;
        t.css(property, minHeight + 'px');
    });
}
})(jQuery);


