﻿var $k = jQuery.noConflict();
// UTILITY TRAY
///////////////////////////////////////
var utility_tray = {
    nav: null,
    opened: false,
    content_wrapper: null,
    content_container: null,
    content: null,
    contentfill: {},
    isshare: false,
    sharename: "share",
    islocations: false,
    locationsname: "locations",
    init: function (content_items) {
        if (content_items) {
            this.contentfill = content_items;
        }
        this.nav = $k('#utility_nav li');
        this.content_wrapper = $k('#utility_content');
        this.nav.children('a').mouseup(function () {
            utility_tray.nav_click($k(this));
            return false;
        });
    },
    get_data: function (data_name) {
        //return utility_data[data_name.toLowerCase()];
        if (this.contentfill[data_name.toLowerCase()]) {
            this.isshare = data_name.toLowerCase() === this.sharename.toLowerCase();
            this.islocations = data_name.toLowerCase() == this.locationsname.toLowerCase();

            return unescape(this.contentfill[data_name.toLowerCase()].replace(/\++/gm, " "));
        }
    },
    nav_click: function (elm) {
        this.content = this.get_data(elm.html());
        if (this.content) {
            $k(elm).click(function () {
                //utility_tray.nav_click($k(this));
                return false;
            });
            $k(elm).parent().parent().children('li').children('a').removeClass('on');
            $k(elm).addClass('on');

            this.content = this.get_data(elm.html());
            if (this.content_wrapper.hasClass('open')) {
                $k('#utility_close').remove();
                this.content_wrapper.animate({ height: 5 }, 500, function () {
                    $k(this).removeClass('open').children().remove();
                    utility_tray.populate_content();
                    utility_tray.open_tray();
                });
            } else {
                utility_tray.populate_content();
                this.open_tray();
            }
        } else {
            this.close_tray();
        }
    },
    populate_content: function () {
        this.content_container = $k('<div id="utility_content_container" />');
        //console.debug(this.content.substring(this.content.search("<script"), this.content.search("</script>")));
        this.content_container.append(this.content);
        this.content_wrapper.append(this.content_container);
        if (this.isshare) {
            var script = 'http://s7.addthis.com/js/250/addthis_widget.js#domready=1&username=quiktrip';
            if (window.addthis) {
                window.addthis = null;
            }
            $k.getScript(script, function () {
                addthis.ost = 0;
                addthis.init();
                name_add_this();
            });
        } else if (this.islocations) {
            var map = $k('#locationsmap').children().click(function () {
                var searchText = $k(this).attr('href').replace("#", "");

                if (searchText != "" && searchText != "enter city and state or zip code") {
                    window.location.href = "/Locations?c=" + encodeURI(searchText);
                }

                return false;
            });
            $k("#SearchLocations").click(function () {
                var searchTextBox = $k("#locationSearchText");
                var searchText = searchTextBox.val();                
                
                if (searchText != "" && searchText != "enter city and state or zip code") {
                    window.location.href = "/Locations?l=" + encodeURI(searchText);
                }

                return false;
            });
        }
    },
    open_tray: function () {
        this.opened = true;
        this.content_wrapper.delay(100).animate({ 'height': utility_tray.content_wrapper.height() }, 0, function () {
            $k(this).addClass('open').animate({ height: utility_tray.content_container.outerHeight() }, 500);
        });
        if ($k('#utility_close').length <= 0) {
            var close_tray_btn = $k('<div id="utility_close" style="cursor:pointer;display:none;">X - Close</div>')
                .css({ position: 'absolute', top: '10px', right: '20px', fontSize: '1.2em', color: '#E1393E' }).prependTo(this.content_wrapper);

            $k('#utility_close').fadeIn(500).click(function () {
                $k(this).remove();
                utility_tray.close_tray();

            });
        }
    },
    close_tray: function (speed) {
        speed = (speed) ? speed : 500;
        this.opened = false;
        utility_tray.content_wrapper.animate({ height: 5 }, speed, function () {
            $k(this).removeClass('open').children().remove();
        });
        utility_tray.nav.children('a').removeClass('on');

        $k('#utility_close').remove();
    }
};

var utility_data = {
    locations: '<a href="#locations" onclick="javascript:$k(\'.focus_cycle\').focusnav(\'activate_link\',$(this)); return false;" class="click"><img alt="Location tray content" src="images/tray_locations.png" /></a>'
	, join: '<a href="connect/index.html" class="click"><img alt="Join tray content" src="images/tray_join.png" /></a>'
	, share: '<img alt="Share tray content" src="images/tray_share.png" />'
};


// MORE
///////////////////////////////////////
(function ($) {
    $.fn.more = function (options) {

        var defaults = {
            moreText: "More Information",
            lessText: "Less Information",
            moreDescription: "Read complete information",
            lessDescription: "Condense information",
            excerpt: "</[\\s?]*p[\\s?]*>",
            ellipsisText: "...",
            contentSelector: "div:first > div:first"
        };

        var options = $.extend(defaults, options);

        return this.each(function () {
            var excerpt_pattern = new RegExp(options.excerpt, "i");

            obj = $(this).find(options.contentSelector);
            var body = obj.html();
            var sep_position = (obj.html().match(excerpt_pattern)) ? obj.html().search(excerpt_pattern) + obj.html().match(excerpt_pattern)[0].length : null;

            if (sep_position) {
                var next = $('<a />');
                var more_wrapper = $('<div class="more_wrapper" />');
                more_wrapper.css({ position: 'relative',overflow:'hidden' });
                var more_container = $('<div class="more_container" />');
                more_container.css({
                    position: 'relative',
                    minWidth: 0,
                    overflow:'hidden'
                });
                next.addClass('more readmore')
                    .attr({ title: options.moreDescription, href: "#" })
                    .text(options.moreText).css({
                        position: 'absolute',
                        bottom: 0,
                        right: 0
                    });
                

                var str1 = body.substring(0, sep_position);
                var str2 = body.substr(sep_position, body.length - 1);
                if (str2.trim().length > 0) {
                    str2 = $.grep($(str2), function (n, i) {
                        if ($(n).text().search(/[a-z]+/i) >= 0) {
                            var x;
                            obj.children().each(function (i, elm) {
                                if ($(elm).html() === $(n).html()) {
                                    x = $(elm);
                                    $(elm).remove();
                                }
                            });
                            return x;
                        }
                    });

                    $(str2).eq(0).css('marginTop', 0);
                    more_container.append(str2);

                    //obj.append(next).append(more_container.hide());
                    obj.append(next).append(more_container);
                    more_container = obj.find('.more_container');   
                    var container_height = more_container.height(); 
                    more_container.hide().height(1);//.css({height:0,width:more_container.width()});                 

                    obj.children().wrapAll(more_wrapper.css({ paddingBottom: next.outerHeight() + 10 }));
                    $(window).focus();

                    obj.find('.more').click(function () {
                        if ( more_container.is(':visible') ) {
                            //more_container.slideUp('slow');
                            more_container.animate({ height: 1 }, 500, function () {
                                more_container.hide().height(1);
                                
                              });
                            $(this).text(options.moreText).attr('title', options.moreDescription);
                        }else{
                            //more_container.slideDown('slow');
                            more_container.show().animate({ height: container_height }, 500)
                            $(this).text(options.lessText).attr('title', options.lessDescription);
                        }
                        return false;
                    });
                    
                }
            }
        });
    };
})(jQuery);

// LIST DRESS
///////////////////////////////////////
(function($) {

    var methods = {
        init: function(options) {
            var defaults = {
                min: 6
            };
            var options = $.extend(defaults, options);


            return this.each(function() {
                var ul = $(this).find('ul');
                ul.each(function(i, elm) {
                    var li = $(elm).children('li');
                    if (li.length >= options.min) {
                        var ul_wrapper = $("<div />");
                        ul_wrapper.addClass('ul_wrapper');
                        $(elm).wrap(ul_wrapper);
                        ul_wrapper.wrap($(elm));

                        var cut = Math.floor(li.length / 2);
                        var endli = li.splice(cut, cut);
                        var newul = $('<ul />');
                        newul.append(endli);

                        ul_wrapper = $(elm).parent();
                        ul_wrapper.append(newul)
                            .css({ borderBottom: 'solid 1px #d9d9d9',borderTop: 'solid 1px #d9d9d9' });

                        var wrapper_width = ul_wrapper.innerWidth();

                        $(elm).parent().children('ul').each(function(i) {
                            var border = (i <= 0) ? 'none' : 'dotted 1px #d9d9d9';
                            $(this).width((wrapper_width / 2) - 20)
                                .css({
                                    borderLeft: border,
                                    verticalAlign : 'top'
                                });
                        });
                    }

                });
            });
        }
    };
    $.fn.listdress = function(method) {
        // Method calling logic
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }

    };
})(jQuery);

// SUMMARY TEASER
///////////////////////////////////////
 (function ($) {

        var methods = {
            init: function (options) {
                var defaults = {
                    imageIndex: 0,
                    container: '.c_content',
                    padding: 10,
                    teaserWidth:219
                };
                var options = $.extend(defaults, options);

                return this.each(function () {
                    var teaser = $(this).find('img').eq(options.imageIndex);
                    if(teaser.length){
                        var teaser_wrapper = $('<div class="teaser_wrapper"></div>');
                        var clear = $('<div />');
                        clear.css({clear:'both'});
                        teaser_wrapper.width(options.teaserWidth)
                            .css({float:'right',marginTop:'-20px',paddingLeft:'10px'});
                    
                        var content_container = $(this).parents(options.container);
                        content_container.children().wrapAll('<div class="teaser_content"></div>');
                        var teaser_content = content_container.children('.teaser_content');
                        teaser_content.width(content_container.width() - options.teaserWidth - options.padding)
                            .css({float:'left'});
                    
                        teaser_wrapper.append(teaser);
                        teaser_content.after(teaser_wrapper,clear);
                    }
                });

            }
        };
        $.fn.summaryteaser = function (method) {
            // Method calling logic
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method ' + method + ' does not exist on jQuery.tooltip');
            }

        };
    })(jQuery);

// FAQ
///////////////////////////////////////
(function ($) {

        var methods = {
            init: function (options) {
                var defaults = {
                    moreText: "See answer",
                    lessText: "Close",
                    ellipsisText: "...",
                    gaqCategory: "_trackEvent",
                    gaqAction:"FAQs",
                    gaqLabels:"View",
                    gaqValue:""
                };

                var options = $.extend(defaults, options);

                return this.each(function() {
                    obj = $(this);
                    

                    //obj.children('dd').slideUp(0);
                    /*.end()
                      .find('dt > a').click(function() {
                          methods.toggle_answer($(this).parent(),options);                          
                          return false;
                      });*/

                      obj.find('dt').each(function(i,elm){ 
                        $(elm).next().children().wrapAll("<div class='faq_content_wrapper' />");//u.css({paddingBottom:'20px'});
                        var dd_height = $(elm).next().height();
                        
                        $(elm).next().height(1).hide().find('.more').eq(0).click(function() {
                              methods.toggle_answer($(this).parent().parent().prev(),options,dd_height);                          
                              return false;
                          });
                          
                          $(elm).children('a').eq(0).click(function() {
                            options.gaqValue = $(this).attr('href');    
                            if(!$(this).parent().next().is(":visible")){
                                googleEventTrack(options.gaqCategory, options.gaqAction, options.gaqLabels, options.gaqValue);                        
                            }
                            methods.toggle_answer($(this).parent(),options,dd_height);                          
                            return false;
                          });
                        /*var next = $("<a />");
                        next.addClass('more readmore')
                            .attr({ href: '#', onclick: $(elm).children('a').eq(0).attr('onclick').toString()})
                            .text(options.moreText).css({
                                position: 'absolute',
                                bottom: -5,
                                right: 0
                            }).click(function () {
                                $(elm).children('a').eq(0).click();
                                return false;
                            });
                        $(this).append(next);*/
                      });
                });

            },
            toggle_answer:function(elm,options,dd_height){ 
            
                var readmore = elm.next().children('.more').eq(0);
                if(elm.next().is(':visible')){
                    //elm.next().hide(600);
                    elm.next().animate({height:1},600,function(){
                        $(this).hide();
                        elm.focus();
                    });
                    //readmore.text(options.moreText);
                }else{
                    //elm.next().show(600);
                    elm.next().show().animate({height:dd_height},600);
                    
                    //readmore.text(options.lessText);
                }
                
            },
            close_answer:function(elm,options){
                
            }
        };
        $.fn.faq = function (method) {
            // Method calling logic
            if (methods[method]) {
                return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
            } else if (typeof method === 'object' || !method) {
                return methods.init.apply(this, arguments);
            } else {
                $.error('Method ' + method + ' does not exist on jQuery.tooltip');
            }

        };
    })(jQuery);



