/* ------------------------------------------------------------------------------------------------------------------------------------
* All plugin are made by Boris Strahija
* Copyright 2009, Boris Strahija
* http://www.creolab.hr/
*
* ------------------------------------------------------------------------------------------------------------------------------------ */

/* ------------------------------------------------------------------------------------------------------------------------------------ */
// !/===> Div link
// searches the element for anchor tags and adds the first one as the click action
/* ------------------------------------------------------------------------------------------------------------------------------------ */
(function($) {
    $.fn.divlink = function(params) {
        // Default options
        var params = jQuery.extend({

    }, params);

    $(this).each(function() {
        var $el = $(this);
        var $first_link = $el.find("a:eq(0)");
        $el.css("cursor", "pointer");
        $el.hover(
				 function() { $(this).addClass("hover"); }
				, function() { $(this).removeClass("hover"); }
			);
        if ($first_link.attr("rel") == "") {
            $(this).click(function() {
                document.location = $($first_link).attr("href");
            });
        }
    }); //end each()

}; //end function()
})(jQuery);



/* ------------------------------------------------------------------------------------------------------------------------------------ */
// !/===> Select Widget
// custom select box
/* ------------------------------------------------------------------------------------------------------------------------------------ */
(function($) {
    $.fn.selectWidget = function(params) {
        // Default options
        var params = jQuery.extend({
            onSelect: false
			 , href: false
			 , extra_class: false
			 , hide_label: false
        }, params);

        $(this).each(function() {
            // Read combo data
            var $combo = $(this);
            var init_label = "-";
            var init_val = "";

            // Hide the combobox
            $combo.hide();

            // First we prepare the required HTML
            var $el = $('<div class="select-widget" style="z-index: ' + $combo.parent().css("z-index") + ';"></div>'); $combo.after($el);
            var $handle = $('<p class="selected"><a href="#"><span>' + init_label + '</span></a></p>'); $el.append($handle);
            var $drop = $('<div class="drop"></div>');
            var $list = $('<ul></ul>');

            // Add extra class
            if (params.extra_class) {
                $el.addClass(params.extra_class);
            } //end if

            // Add options
            $combo.children('option').each(function() {
                // Selected class
                var sel_class = ' class="';
                if ($(this).attr("value") == $combo.val()) sel_class += 'sel';
                if ($(this).hasClass("label")) sel_class += ' label';
                sel_class += '"';

                // Prepare HTML
                var html = $(this).text();
                html = html.replace('*(', '<strong>(');
                html = html.replace(')*', ')</strong>');
                html = html.replace('_(', '<em>(');
                html = html.replace(')_', ')</em>');

                // Append to list
                $list.append('<li' + sel_class + '><a href="#" rel="' + $(this).attr("value") + '">' + html + '</a></li>');
            });

            // And add to list
            $drop.append($list);
            $el.append($drop);

            // Rest of options
            var href = params.href;
            var selected_value = 0;
            var init = true;

            // Hide the list and align it
            $drop.hide().css({
                top: $handle.height() - 2
            });

            // Add click event on handle
            $handle.children("a").click(function() {
                $el.toggleClass("select-widget-open");
                $drop.toggle().css({
                    top: $handle.height() - 2
                });
                $(".select-widget").not($el).removeClass("select-widget-open");
                $(".select-widget .drop").not($drop).hide();
                return false;
            });

            // Add click event on select elements
            $list.children("li").children("a").click(function() {
                var label = $(this).html();
                selected_value = $(this).attr("rel");

                // Hide the label
                if (params.hide_label) {
                    $thisli = $(this).parent("li");
                    if (!$thisli.hasClass("label")) {
                        $list.children("li.label").hide();
                    } //end if
                } //end if

                // Set values
                $handle.children("a").children("span").html(label);
                $combo.val(selected_value);

                // Hide the list and open class
                $el.removeClass("select-widget-open");
                $drop.hide();
                if (init === false) $combo.change();

                // Redirect on select
                if (init == false && params.onSelect == "submit" && href) {
                    document.location = href + "?" + $combo.attr("name") + "=" + selected_value;
                } // end if

                return false;
            });

            // Select initial if it exists
            $list.children("li.sel").children("a").click();

            // Hide label from selection
            if (params.hide_label) {
                $list.children("li.label").remove();
            } //end if

            // Reset init
            init = false;

            // Add blur action
            $el.blur(function() {
                $el.removeClass("select-widget-open");
                $drop.hide();
            });

        }); //end each()

    }; //end selectWidget()
})(jQuery);



/* ------------------------------------------------------------------------------------------------------------------------------------
/* !/===> Tabs */
/* ------------------------------------------------------------------------------------------------------------------------------------ */
(function($) {
    $.fn.tabbed = function(params) {
        // Default options
        var params = jQuery.extend({
            hashnav: true
			  , scroll: false
        }, params);

        $(this).each(function() {
            var el = $(this);

            // tabs count
            var tabsCount = $("#hiddenTabsCount").val();

            // Init marker
            $(el).find(".tabs a").css("outline", "none");
            $(el).find(".tabs a").removeClass("on");
            $(el).parent().parent().find(".tabs h3").removeClass("on");
            $(el).find(".tab-cont > .cx").hide();

            // Init content
            var tab_hash = hash.split("-");

            // if page reloads, the current tab is active
            if (hash == "") {
                if (params.hashnav && tab_hash && tab_hash[0] && tab_hash[1] && tab_hash[0] == 'tabbed') {
                    $(el).find(".tabs a:eq(" + tab_hash[1] + ")").addClass("on");
                    $(this).parent().parent().find(".tabs h3:eq(" + tab_hash[1] + ")").addClass("on");
                    $(el).find(".tab-cont > .cx:eq(" + tab_hash[1] + ")").show();
                } else {
                    $(el).find(".tabs a:eq(0)").addClass("on");
                    $(this).parent().parent().find(".tabs h3:eq(0)").addClass("on");
                    $(el).find(".tab-cont > .cx:eq(0)").show();
                } //end if
            } else {
                var $tab = $(el).find(".tabs a:eq(" + tab_hash[1] + ")");
                if ($tab.length > 0) {
                    $tab.addClass("on");
                    $(this).parent().parent().find(".tabs h3:eq(" + tab_hash[1] + ")").addClass("on");
                    $(el).find(".tab-cont > .cx:eq(" + tab_hash[1] + ")").show();
                } else {
                    $(el).find(".tabs a:eq(0)").addClass("on");
                    $(this).parent().parent().find(".tabs h3:eq(0)").addClass("on");
                    $(el).find(".tab-cont > .cx:eq(0)").show();
                }
            }

            for (var i = 0; i <= tabsCount - 1; i++) {
                if (tab_hash[1] == i.toString()) {
                    if (params.hashnav && tab_hash && tab_hash[0] && tab_hash[1] && tab_hash[0] == 'tabbed') {
                        $(el).find(".tabs a:eq(" + tab_hash[1] + ")").addClass("on");
                        $(this).parent().parent().find(".tabs h3:eq(" + i.toString() + ")").addClass("on");
                        $(el).find(".tab-cont > .cx:eq(" + tab_hash[1] + ")").show();
                    } else {
                        $(el).find(".tabs a:eq(" + i.toString() + ")").addClass("on");
                        $(this).parent().parent().find(".tabs h3:eq(" + i.toString() + ")").addClass("on");
                        $(el).find(".tab-cont > .cx:eq(" + i.toString() + ")").show();
                    } //end if
                } //end if
            } // end for

            // Fix tabs in IE6
            //if ($.browser.msie) $(el).find(".tabs a em").wrapInner('<span></span>');

            // Add click events
            $(el).find(".tabs a").each(function(num) {
                $(this).click(function() {
                    // Marker
                    $(el).find(".tabs a").removeClass("on");
                    $(el).parent().parent().find(".tabs h3").removeClass("on");
                    $(this).addClass("on");
                    $(this).parent().addClass("on");

                    // Content
                    $(el).find(".tab-cont .cx").hide();
                    $(el).find(".tab-cont .cx:eq(" + num + ")").show();

                    // Set URL hash
                    if (params.hashnav) document.location.hash = "#tabbed-" + num;

                    return false;
                }); //end click()
            }); //end each()
        }); // end each()
    }; //end function()
})(jQuery);







/* ------------------------------------------------------------------------------------------------------------------------------------
* Equal height columns
* ------------------------------------------------------------------------------------------------------------------------------------ */
(function($) {
    $.fn.equalHeight = function(substract) {
        var tallest = 0;
        $(this).each(function() {
            var $el = $(this);
            var this_height = $el.height();
            if ($.browser.msie || $.browser.opera) this_height = $el.innerHeight();
            if (this_height > tallest) {
                tallest = this_height;
            } //end if
        });
        if (substract) tallest -= substract;
        //$(this).height(tallest);
        $(this).css({ "height": tallest + "px" });
    };
    /* ------------------------------------------------------------------------------------------------------------------------------------
    * drop down - Height
    * ------------------------------------------------------------------------------------------------------------------------------------ */

    $.fn.ddHeight = function(add) {


        return $(this).each(function() {

            var maxheight = 0;

            $(this).find("ul").each(function() {

                if (maxheight < $(this).height()) {
                    maxheight = $(this).height();

                }
            });


            $(this).css({ height: maxheight + add });
        });
    };

    /* ------------------------------------------------------------------------------------------------------------------------------------
    * dd - maxheight
    * ------------------------------------------------------------------------------------------------------------------------------------ */

    $.fn.ddMaxHeight = function(add) {

        var maxheight = 0;

        $(this).each(function() {

            $(this).find("ul").each(function() {

                if (maxheight < $(this).height()) {
                    maxheight = $(this).height();

                }
            });

        });
        return maxheight + add;
    };

})(jQuery);



/* ------------------------------------------------------------------------------------------------------------------------------------
* Console log
* ------------------------------------------------------------------------------------------------------------------------------------ */
// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function() {
    log.history = log.history || [];   // store logs to an array for reference
    log.history.push(arguments);
    if (this.console) {
        console.log(Array.prototype.slice.call(arguments));
    }
};




/* ------------------------------------------------------------------------------------------------------------------------------------
* Jcarousel Lite
* ------------------------------------------------------------------------------------------------------------------------------------ */
(function($) {
    $.fn.jCarouselLite = function(o) {
        o = $.extend({ btnPrev: null, btnNext: null, btnGo: null, mouseWheel: false, auto: null, speed: 200, easing: null, vertical: false, circular: true, visible: 3, start: 0, scroll: 1, beforeStart: null, afterEnd: null }, o || {}); return this.each(function() {
            var running = false, animCss = o.vertical ? "top" : "left", sizeCss = o.vertical ? "height" : "width"; var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible; if (o.circular) { ul.prepend(tLi.slice(tl - v - 1 + 1).clone()).append(tLi.slice(0, v).clone()); o.start += v; }
            var li = $("li", ul), itemLength = li.size(), curr = o.start; div.css("visibility", "visible"); li.css({ overflow: "hidden", float: o.vertical ? "none" : "left" }); ul.css({ margin: "0", padding: "0", position: "relative", "list-style-type": "none", "z-index": "1" }); div.css({ overflow: "hidden", position: "relative", "z-index": "2", left: "0px" }); var liSize = o.vertical ? height(li) : width(li); var ulSize = liSize * itemLength; var divSize = liSize * v; li.css({ width: li.width(), height: li.height() }); ul.css(sizeCss, ulSize + "px").css(animCss, -(curr * liSize)); div.css(sizeCss, divSize + "px"); if (o.btnPrev)
                $(o.btnPrev).click(function() { return go(curr - o.scroll); }); if (o.btnNext)
                $(o.btnNext).click(function() { return go(curr + o.scroll); }); if (o.btnGo)
                $.each(o.btnGo, function(i, val) { $(val).click(function() { return go(o.circular ? o.visible + i : i); }); }); if (o.mouseWheel && div.mousewheel)
                div.mousewheel(function(e, d) { return d > 0 ? go(curr - o.scroll) : go(curr + o.scroll); }); if (o.auto)
                setInterval(function() { go(curr + o.scroll); }, o.auto + o.speed); function vis() { return li.slice(curr).slice(0, v); }; function go(to) {
                    if (!running) {
                        if (o.beforeStart)
                            o.beforeStart.call(this, vis()); if (o.circular) { if (to <= o.start - v - 1) { ul.css(animCss, -((itemLength - (v * 2)) * liSize) + "px"); curr = to == o.start - v - 1 ? itemLength - (v * 2) - 1 : itemLength - (v * 2) - o.scroll; } else if (to >= itemLength - v + 1) { ul.css(animCss, -((v) * liSize) + "px"); curr = to == itemLength - v + 1 ? v + 1 : v + o.scroll; } else curr = to; } else { if (to < 0 || to > itemLength - v) return false; else curr = to; }
                        running = true; ul.animate(animCss == "left" ? { left: -(curr * liSize)} : { top: -(curr * liSize) }, o.speed, o.easing, function() {
                            if (o.afterEnd)
                                o.afterEnd.call(this, vis()); running = false;
                        }); if (!o.circular) { $(o.btnPrev + "," + o.btnNext).removeClass("disabled"); $((curr - o.scroll < 0 && o.btnPrev) || (curr + o.scroll > itemLength - v && o.btnNext) || []).addClass("disabled"); }
                    }
                    return false;
                };
        });
    }; function css(el, prop) { return parseInt($.css(el[0], prop)) || 0; }; function width(el) { return el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight'); }; function height(el) { return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom'); };
})(jQuery);
