//Integra Jquery
//Date: 2010
//Sequence.co.uk
//Client: Integra
//Owain Lewis.

$.fn.hasAttr = function(name) {
    return this.attr(name) !== undefined;
};

//jQuery Truncate Text Plugin
//Owain Lewis
//URL: www.owainlewis.com

(function($) {
    $.fn.extend({
        truncate: function(options) {
            var defaults = {
                limit: 100,
                delimiter: '...'
            };
            var options = $.extend(defaults, options);
            String.prototype.trunc = function(n) {
                return this.substr(0, n - 1) + (this.length > n ? options.delimiter : '');
            };
            return this.each(function(limit) {
                var obj = jQuery(this);
                string = jQuery(this).html();
                newString = string.trunc(options.limit);
                obj.html(newString);
            });
        }
    });
})(jQuery);

function printPage(){
    window.print();
}

////////////////////////////////////////////////////////////////////////////////
//BrowserFix JS
///////////////////////////////////////////////////////////////////////////////

//Apply unique styles to different browser versions using Jquery or Javascript
//2010 Owain Lewis
//www.Owainlewis.com
//Provides detection for all major Browsers.
//Not only can this script detect your users web browser, it can also detect
//the browser version which can be userful in targeting modern CSS3 and HTML5
//techniques

///////////////////////////////////////////////////////////////////////////////

var BrowserFix = {
    init: function() {
        this.browser = this.searchString(this.browserDataArray) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },

    searchString: function(data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function(dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    //Check if cookies are enabled. Returns true or false
    cookies: function() {
        this.enabled = navigator.cookieEnabled;
        return this.enabled;
    },
    browserDataArray: [
		{ string: navigator.userAgent, subString: "Chrome", identity: "Chrome" },
		{ string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" },
		{ prop: window.opera, identity: "Opera" },
		{ string: navigator.userAgent, subString: "Firefox", identity: "Firefox" },
		{ string: navigator.userAgent, subString: "MSIE", identity: "Internet Explorer", versionSearch: "MSIE" },
		{ string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" },
		],
    dataOS: [
		  { string: navigator.platform, subString: "Win", identity: "Windows" },
		{ string: navigator.platform, subString: "Mac", identity: "Mac" },
		{ string: navigator.userAgent, subString: "iPhone", identity: "iPhone/iPod" },
		{ string: navigator.platform, subString: "Linux", identity: "Linux" }
	]
};

BrowserFix.init();

var MODAL = {
    modalsize: 200,
    setup: function() {
        var leftPos = $(window).width() / 2;
        var height = $(document).height();
        var width = $(document).width();
        jQuery('#modalWindow').css('left', leftPos - this.modalsize);
        jQuery('#modalMask').css('width', width);
        jQuery('#modalMask').css('height', height);
    },
    popup: function() {
        this.scrollTop();
        jQuery('#modalMask').show();
        jQuery('#modalWindow').fadeIn()
    },
    buildContent: function(html) {
        var el = document.getElementById('modalContent');
        el.innerHTML = "<h1>Carriage Terms and Conditions</h1>" + html;
    },
    closePopup: function() {
        jQuery('#modalMask').hide();
        jQuery('#modalWindow').fadeOut();
    },
    scrollTop: function(speed) {
        var s = arguments[0] | 400;
        $('html,body').animate({ scrollTop: 0 }, s);
    }
}



//////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function() {

    //Firefox 3.5   
    if (BrowserFix.browser === 'Firefox' && BrowserFix.version == '3.5') {
        jQuery('#nav li').each(function() {
            jQuery(this).css('padding', '0 9px 0 7px');
        });
        //Conditional fix for firefox 3.5 bug on homepage
        if (jQuery('.four_col_b').length) {
            jQuery('.four_col_b').css('width', '175px');
        }
    }

    $('#supply1 a').hover(function(e) {
        var targetId = "item" + $(this).attr('id');
        MODAL.setup();
        MODAL.buildContent.call(this,document.getElementById(targetId).innerHTML);
        MODAL.popup();
        return false;
    });

    $('#modalMask,#closeBtn').live('click', function() {
        MODAL.closePopup.call(this);
    });

    $('#supply a').hover(function(e) {
        var targetId = "item" + $(this).attr('id');
        MODAL.setup();
        MODAL.buildContent.call(this, document.getElementById(targetId).innerHTML);
        MODAL.popup();
        return false;
    });

    $('#modalMask,#closeBtn').live('click', function() {
        MODAL.closePopup.call(this);
    });

    //Strip elements from calendar 
    if (jQuery('.sm_calendar').size()) {
        elements = ['p', 'h5', 'a', 'h2']
        for (i = 0; i < elements.length; i++) {
            _target = '.event_main ' + elements[i];
            jQuery(_target).css('display', 'none');
        }
    }

    //Only allow two home page carousel items
    $('.cta_nav li.next').eq(1).hide();
    $('.cta_nav li.next').eq(2).hide();
    $('.cta_nav li.next').eq(3).hide();

    //Slideshow for inner pages
    if ($('.slideshow2').length) { //If the slideshow2 class is present on the page...
        $('.slideshow2').cycle({
            fx: 'scrollRight',
            easing: '',
            speed: 1000,
            timeout: 7000
        });
    }

    //Modal Dialog Window
    $('a.modal').click(function(e) {
        e.preventDefault();
        $('html, body').animate({ scrollTop: 0 }, 400);
        var id = $(this).attr('href');
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
        $('#mask').show();
        $('#mask').fadeTo(100, 0.8);
        var winH = $(window).height();
        var winW = $(window).width();
        $(id).css('top', 0);
        $(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).show().delay(200).animate(
        { top: '200px' }, {
            duration: 1000,
            easing: 'easeOutBack'
        });
        return false;

    });

    jQuery('.print a').click(function() {
        window.print();
    });

    //Make the first tab active and remove the active class from the rest
    $('#tab2').removeClass('active');
    $('#tab3').removeClass('active');
    $('#tab4').removeClass('active');
    $('#tab5').removeClass('active');

    //Page Tabs Effect
    $('#tab1').click(function(e) {
        e.preventDefault();
        $('#tab2_content').hide().removeClass('active');
        $('#tab3_content').hide().removeClass('active');
        $('#tab4_content').hide().removeClass('active');
        $('#tab5_content').hide().removeClass('active');
        $('li#tab2').removeClass('active');
        $('li#tab3').removeClass('active');
        $('li#tab4').removeClass('active');
        $('li#tab5').removeClass('active');
        $('li#tab1').addClass('active');
        $('#tab1_content').fadeIn(700);
    });

    $('#tab2').click(function(e) {
        e.preventDefault();
        $('#tab1_content').hide();
        $('#tab3_content').hide();
        $('#tab4_content').hide();
        $('#tab5_content').hide();
        $('li#tab1').removeClass('active');
        $('li#tab3').removeClass('active');
        $('li#tab4').removeClass('active');
        $('li#tab5').removeClass('active');
        $('li#tab2').addClass('active');
        $('#tab2_content').fadeIn(700);

    });

    $('#tab3').click(function(e) {
        e.preventDefault();
        $('#tab1_content').hide().removeClass('active');
        $('#tab2_content').hide().removeClass('active');
        $('#tab4_content').hide().removeClass('active');
        $('#tab5_content').hide().removeClass('active');
        $('li#tab1').removeClass('active');
        $('li#tab2').removeClass('active');
        $('li#tab4').removeClass('active');
        $('li#tab5').removeClass('active');
        $('li#tab3').addClass('active');
        $('#tab3_content').fadeIn(700);

    });

    $('#tab4').click(function(e) {
        e.preventDefault();
        $('#tab1_content').hide().removeClass('active');
        $('#tab2_content').hide().removeClass('active');
        $('#tab3_content').hide().removeClass('active');
        $('#tab5_content').hide().removeClass('active');
        $('li#tab1').removeClass('active');
        $('li#tab2').removeClass('active');
        $('li#tab3').removeClass('active');
        $('li#tab5').removeClass('active');
        $('li#tab4').addClass('active');
        $('#tab4_content').fadeIn(700);

    });

    $('#tab5').click(function(e) {
        e.preventDefault();
        $('#tab1_content').hide().removeClass('active');
        $('#tab2_content').hide().removeClass('active');
        $('#tab3_content').hide().removeClass('active');
        $('#tab4_content').hide().removeClass('active');
        $('li#tab1').removeClass('active');
        $('li#tab2').removeClass('active');
        $('li#tab3').removeClass('active');
        $('li#tab4').removeClass('active');
        $('li#tab5').addClass('active');
        $('#tab5_content').fadeIn(700);

    });

    $('.slideTop').click(function(e) {
        e.preventDefault;
        $('html, body').animate({ scrollTop: 0 }, { duration: 1000, easing: 'easeInOutSine' });
        return false;
    });

    //Level three menu
    if ($.browser.msie && parseFloat($.browser.version) < 8) {
        jQuery('#left_col ul.level_3').css('display', 'block');
        jQuery('#left_col ul.level_4').css('display', 'block');
        return;
    }

    if ($('#left_col ul').length) {
        $('#left_col ul.level_3').each(function(index) {
            $(this).parent('li').find('a').first().mouseenter(function(e) {
                $('#left_col ul.level_3').eq(index).slideDown(800);
                return false;
            });
        });
        $('#left_col ul.level_4').each(function(index) {
            $(this).parent('li').find('a').first().mouseenter(function(e) {
                $('#left_col ul.level_4').eq(index).slideDown(800);
                return false;
            });
        });
        $('#left_col').mouseleave(function(e) {
            $('#left_col ul.level_3').slideUp(300);
            $('#left_col ul.level_4').slideUp(300);
            return false;
        });
    }

    //Break pager if number of list items is too long

    if ($('.pager ul li').size() > 29) {
        $('.pager ul li').eq(28).append('<br/>')
    }

    //Initiative menu item styled via Jquery
    $('#nav ul li').eq(4).addClass('init');

    //Make the whole parent nav li area clickable
    $('#nav ul li').each(function() {
        $(this).click(function() {
            window.location = $(this).children('a').attr('href');
        });
    });

    $("select,input:checkbox, input:radio, input:file").uniform();

    //Find every table and strip cellpadding and cellspacing to validate as HTML5
    $('table').each(function(index) {
        if ($(this).hasAttr('cellpadding')) {
            $(this).removeAttr('cellpadding');
        }
        if ($(this).hasAttr('cellpadding')) {
            $(this).removeAttr('cellpadding');
        }
    });

    //$('#carousel_ul h2 a').css('font-size', '22px');
    //$('#carousel_ul h2 a').css('color', '#000');
    //Cufon.replace('#carousel_ul h2 a');

    //Simple Tool Tips
    xOffset = 10;
    yOffset = 20;
    $('.contact li').each(function(e) {
        var $this = $(this);
        if ($this.hasClass('phone')) {
            $this.append("<span class='plus'><img src='/images/add.png' /></span>");
        }
        $(this).css('cursor', 'pointer').hover(function() {
            var html = $this.text();
            $("body").append("<div id='tooltip'>" + html + "</div>");
            $("#tooltip")
	            .css("top", (e.pageY) + "px")
	            .css("left", (e.pageX + yOffset) + "px")
	            .fadeIn("slow");
        }, function() {
            $("#tooltip").remove();
        });

    });

    $(".contact li").mousemove(function(e) {
        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });

    //Simple Tool Tips - Other pages
    xOffset = 10;
    yOffset = 20;
    $('.contactpage').each(function(e) {
        var $this = $(this);
        if ($this.hasClass('phone')) {
            $this.append("<span class='plus'><img src='/images/add.png' /></span>");
        }
        $(this).css('cursor', 'pointer').hover(function() {
            var html = $this.text();
            $("body").append("<div id='tooltip'>" + html + "</div>");
            $("#tooltip")
	            .css("top", (e.pageY) + "px")
	            .css("left", (e.pageX + yOffset) + "px")
	            .fadeIn("slow");
        }, function() {
            $("#tooltip").remove();
        });

    });

    $(".contactpage").mousemove(function(e) {
        $("#tooltip")
			.css("top", (e.pageY - xOffset) + "px")
			.css("left", (e.pageX + yOffset) + "px");
    });


    //Amy Filter Menu JS

    $(".filterbtn").click(function() {
        $("#search_filters").toggle();
        $(".filterbtn").toggleClass("menu-open");
    });

    $("#search_filters").mouseup(function() {
        return false
    });
    $(document).mouseup(function(e) {
        if ($(e.target).parent("a.filterbtn").length == 0) {
            $(".filterbtn").removeClass("menu-open");
            $("#search_filters").hide();
        }
    });

    $("#search_filters ul").hide();

    $("#search_filters h4").click(function() {
        $(this).next("#search_filters ul").slideToggle(250);
    });

    //Fix pagination
    var curListItem = jQuery('.pager ul .active');
    var getNextHref = curListItem.next('li').children('a').attr('href');
    var getPrevHref = curListItem.prev('li').children('a').attr('href');
    var nextButton = jQuery('.pager ul .next');
    var prevButton = jQuery('.pager ul .prev');

    nextButton.click(function() {
        window.location = getNextHref;
    });
    prevButton.click(function() {
        window.location = getPrevHref;
    });

    //End document ready

});


