//REPLACE CHARACTERS
$(document).ready(function() {
    $("p").each(function() {
        $(this).html($(this).html().replace("ÃƒÂ¢Ã¢â€šÂ¬Ã¢â€žÂ¢", "'"));
        $(this).html($(this).html().replace("ÃƒÂ¢Ã¢â€šÂ¬Ã…â€œ", "'")); 
		$(this).html($(this).html().replace("ÃƒÂ¢Ã¢â€šÂ¬Ã‚Â", "'")); 
		$(this).html($(this).html().replace("ÃƒÂ¢Ã¢â€šÂ¬Ã¢â€žÂ¢", "'"));
    });

});

//ALTERNATE CLASSES
$(document).ready(function() {
    $("table tr:even").addClass("even");
    $("table tr:odd").addClass("odd");
});


$(document).ready(function() {

    //LUSTER SELECTIONS
    $('#pricing_luster').children('option:not([id="5"] , [id="6"], [id="7"])').remove();

    //LENGTH SELECTIONS
    $('#pricing_length').children('option:not([id="10"] , [id="11"], [id="12"], [id="13"], [id="14"], [id="15"], [id="16"], [id="17"], [id="18"], [id="19"], [id="20"], [id="21"], [id="22"])').remove();

    //PATTERN SELECTIONS
    $('#pricing_pattern').children('option:not([id="4"], [id="8"], [id="9"], [id="23"] , [id="24"], [id="25"], [id="35"], [id="36"])').remove();

    //COLOR SELECTIONS
    $('#pricing_color').children('option:not([id="32"] , [id="33"])').remove();

    //LAYER LUSTER SELECTIONS
    $('#layer_luster').children('option:not([id="5"] , [id="6"], [id="7"])').remove();

    //LAYER LENGTH SELECTIONS
    $('#layer_length').children('option:not([id="10"] , [id="11"], [id="12"], [id="13"], [id="14"], [id="15"], [id="16"], [id="17"], [id="18"], [id="19"], [id="20"], [id="21"], [id="22"])').remove();

    //LAYER PATTERN SELECTIONS
    $('#layer_pattern').children('option:not([id="4"], [id="8"], [id="9"], [id="23"] , [id="24"], [id="25"], [id="35"], [id="36"])').remove();

    //LAYER COLOR SELECTIONS
    $('#layer_color').children('option:not([id="32"] , [id="33"])').remove();

});

//MENU
$(document).ready(function() {

    $("ul.nav li a").hover(function() {
        //When trigger is clicked...
        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).parent().find("ul.subnav").slideDown('fast').show();
        //Drop down the subnav on click
        $(this).parent().hover(function() {
            },
        function() {
            $(this).parent().find("ul.subnav").slideUp('slow');
            //When the mouse hovers out of the subnav, move it back up
        });

        //Following events are applied to the trigger (Hover events for the trigger)
    }).hover(function() {
        $(this).addClass("subhover");
        //On hover over, add class "subhover"
    },
    function() {
        //On Hover Out
        $(this).removeClass("subhover");
        //On hover out, remove class "subhover"
    });

});

//ADD CLASS
$(document).ready(function() {
    $("ul.children li:first-child").addClass("first");
});


//TOOLTIP
$(document).ready(function() {
    // initialize tooltip
    $(".tooltip[title]").tooltip({

        // tweak the position
        offset: [10, 2],

        // use the "slide" effect
        effect: 'slide'

        // add dynamic plugin with optional configuration for bottom edge
    }).dynamic({
        bottom: {
            direction: 'down',
            bounce: true
        }
    });

});

//SCROLLER
$(function()
 {
    $('.social_comments').jScrollPane();
});


//ALTERNATE SCRIPT
$(document).ready(function() {
    $("table.default.cart tr:even").addClass("even");
    $("table.default.cart tr:odd").addClass("odd");
    $("table.default.cart tr:last").addClass("last");
});

//ALTERNATE SCRIPT
$(document).ready(function() {
    $("table.workflow tr:last").addClass("last");
    $("ul.commentlist li:last").addClass("last");
});


//Gallery SLIDES
$(document).ready(function() {
    $('#gallery').slides({
        preload: true
        //preloadImage: 'img/loading.gif',
        //play: 5000,
        //pause: 2500,
        //hoverPause: true
    });
});

//TOUR SLIDES
$(document).ready(function() {
    $('#slides').slides({
        preload: true,
        //preloadImage: 'img/loading.gif',
        play: 5000,
        pause: 2500,
        hoverPause: true
    });
});

//SCROLL BACK TO TOP WHEN SUBMIT
$(document).ready(function() {
    // scroll to top
    $('a.submit').click(function() {
        $.scrollTo(300, 800);
        return true;
    });
    $('a.top').click(function() {
        $.scrollTo(250, 800);
        return true;
    });
});


// VALIDATION FORM
/* <![CDATA[ */
$(function() {
    //REGISTRATION & EDIT PROFILE
    $("#valid_username").validate({
        expression: "if (VAL) return true; else return false;",
        message: "Please enter a username"
    });
    $("#valid_firstname").validate({
        expression: "if (VAL) return true; else return false;",
        message: "Please enter your first name"
    });
    $("#valid_lastname").validate({
        expression: "if (VAL) return true; else return false;",
        message: "Please enter your last name"
    });
    $("#valid_number").validate({
        expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
        message: "Should be a number"
    });
    $("#valid_phone").validate({
        expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
        message: "Please enter valid phone number no spaces - or ."
    });
    $("#valid_date").validate({
        expression: "if (!isValidDate(parseInt(VAL.split('/')[2]), parseInt(VAL.split('/')[0]), parseInt(VAL.split('/')[1]))) return false; else return true;",
        message: "Please enter a valid Date"
    });
    $("#valid_password").validate({
        expression: "if (VAL.length > 5 && VAL) return true; else return false;",
        message: "Please enter a valid password"
    });
    $("#valid_password_confirm").validate({
        expression: "if ((VAL == jQuery('#valid_password').val()) && VAL) return true; else return false;",
        message: "Please confirm password"
    });
    $("#valid_address").validate({
        expression: "if (VAL) return true; else return false;",
        message: "Please enter your address"
    });
    $("#valid_city").validate({
        expression: "if (VAL) return true; else return false;",
        message: "Please enter a city"
    });
    $("#valid_state").validate({
        expression: "if (VAL) return true; else return false;",
        message: "Please enter a state"
    });
    $("#valid_zip").validate({
        expression: "if (!isNaN(VAL) && VAL) return true; else return false;",
        message: "Please enter a valid zip"
    });
    $("#valid_email").validate({
        expression: "if (VAL.match(/^[^\\W][a-zA-Z0-9\\_\\-\\.]+([a-zA-Z0-9\\_\\-\\.]+)*\\@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)*\\.[a-zA-Z]{2,4}$/)) return true; else return false;",
        message: "Please enter a valid email"
    });
    $("#valid_selection").validate({
        expression: "if (VAL != '0') return true; else return false;",
        message: "Please make a selection"
    });
    $("#valid_country").validate({
        expression: "if (VAL != '0') return true; else return false;",
        message: "Please select a country"
    });
    $("#valid_timezone").validate({
        expression: "if (VAL != '0') return true; else return false;",
        message: "Please select a time zone"
    });
    $("#valid_privacy").validate({
        expression: "if (isChecked(SelfID)) return true; else return false;",
        message: "Please read our privacy policy"
    });
    $("#valid_shipping").validate({
        expression: "if (isChecked(SelfID)) return true; else return false;",
        message: "Please read our shipping policy"
    });
    $("#valid_return").validate({
        expression: "if (isChecked(SelfID)) return true; else return false;",
        message: "Please read our return policy"
    });
    $("#ValidCheckbox").validate({
        expression: "if (isChecked(SelfID)) return true; else return false;",
        message: "Please check atleast one checkbox"
    });
});
/* ]]> */

//BUCKETS - ACCORDION
$(function() {
    $("#accordion").tabs("#accordion div.pane", {
        tabs: 'h4.nav',
        effect: 'slide',
        initialIndex: null
    });
});
$(document).ready(function() {
    $('.pane').hide();
    $('.pane.first').show();
    return false;
});


//TABS
$(function() {
    $(".nav_profile").tabs(".tabs_profile > div", {
        effect: 'default'
    });
});

$(function() {
    $(".nav_rewards").tabs(".tabs_rewards > div", {
        effect: 'default'
    });
});

$(function() {
    $(".nav_guide").tabs(".tabs_guide > div", {
        effect: 'fade',
        fadeInSpeed: 500
    });
});

$(function() {
    // setup ul.tabs to work as tabs for each div directly under div.panes
    $("ul.tabs").tabs("div.panes > div");
});

//FADES
$(document).ready(
function() {
    $('ul#panels').innerfade({
        speed:
        1000,
        timeout: 5000,
        type: 'sequence',
        containerheight: '882'
    });

});


// TOGGLE DIVS
$(document).ready(function() {
    $('.info').hide();
    $('div.category div.item').hide();
    return false;
});

$(document).ready(function() {
    $('h4.toggle').click(function() {
        $(this).next().animate({
            height: 'toggle'
        }).delay(800);
        return false;
    });
});


$(document).ready(function() {
    $('.info').click(function() {
        $(this).prev().animate({
            height: 'toggle'
        },
        200).delay(800);
        return false;
    });
});



//FADE BASE CODE
// wrap as a $ plugin and pass $ in to our anoymous function
 (function($) {
    $.fn.cross = function(options) {
        return this.each(function(i) {
            // cache the copy of $(this) - the start image
            var $$ = $(this);

            // get the target from the backgroundImage + regexp
            var target = $$.css('backgroundImage').replace(/^url|[\(\)'"]/g, '');

            // nice long chain: wrap img element in span
            $$.wrap('<span style="position: relative;"></span>')
            // change selector to parent - i.e. newly created span
            .parent()
            // prepend a new image inside the span
            .prepend('<img>')
            // change the selector to the newly created image
            .find(':first-child')
            // set the image to the target
            .attr('src', target);

            // the CSS styling of the start image needs to be handled
            // differently for different browsers
            if ($.browser.mozilla) {
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': '',
                    'top': this.offsetTop

                });

            } else if ($.browser.msie && ($.browser.version < 7.9999)) {
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': '',
                    'top': 0

                });
            } else if ($.browser.opera && $.browser.version < 9.5) {
                // Browser sniffing is bad - however opera < 9.5 has a render bug
                // so this is required to get around it we can't apply the 'top' : 0
                // separately because Mozilla strips the style set originally somehow...
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': '',
                    'top': "0"
                });
            } else {
                // Safari
                $$.css({
                    'position': 'absolute',
                    'left': 0,
                    'background': ''
                });
            }

            // similar effect as single image technique, except using .animate
            // which will handle the fading up from the right opacity for us
            $$.hover(function() {
                $$.stop().animate({
                    opacity: 0
                },
                200);
            },
            function() {
                $$.stop().animate({
                    opacity: 1
                },
                400);
            });
        });
    };

})($);

// note that this uses the .bind('load') on the window object, rather than $(document).ready()
// because .ready() fires before the images have loaded, but we need to fire *after* because
// our code relies on the dimensions of the images already in place.
$(window).bind('load',
function() {
    $('img.fade').cross();
});


//VALIDATE BASE CODE
/**
	 * @author GeekTantra
	 * @date 20 September 2009
	 */

function isChecked(id) {
    var ReturnVal = false;
    $("#" + id).find('input[type="radio"]').each(function() {
        if ($(this).is(":checked"))
        ReturnVal = true;
    });
    $("#" + id).find('input[type="checkbox"]').each(function() {
        if ($(this).is(":checked"))
        ReturnVal = true;
    });
    return ReturnVal;
}

/*
	 * This functions checks where an entered date is valid or not.
	 * It also works for leap year feb 29ths.
	 * @year: The Year entered in a date
	 * @month: The Month entered in a date
	 * @day: The Day entered in a date
	 */
function isValidDate(year, month, day) {
    var date = new Date(year, (month - 1), day);
    var DateYear = date.getFullYear();
    var DateMonth = date.getMonth();
    var DateDay = date.getDate();
    if (DateYear == year && DateMonth == (month - 1) && DateDay == day)
    return true;
    else
    return false;
}


 (function($) {
    var ValidationErrors = new Array();
    $.fn.validate = function(options) {
        options = $.extend({
            expression: "return true;",
            message: "",
            error_class: "ValidationErrors",
            error_field_class: "ErrorField",
            live: true
        },
        options);
        var SelfID = $(this).attr("id");
        var unix_time = new Date();
        unix_time = parseInt(unix_time.getTime() / 1000);
        if (!$(this).parents('form:first').attr("id")) {
            $(this).parents('form:first').attr("id", "Form_" + unix_time);
        }
        var FormID = $(this).parents('form:first').attr("id");
        if (! ((typeof(ValidationErrors[FormID]) == 'object') && (ValidationErrors[FormID] instanceof Array))) {
            ValidationErrors[FormID] = new Array();
        }
        if (options['live']) {
            if ($(this).find('input').length > 0) {
                $(this).find('input').bind('blur',
                function() {
                    if (validate_field("#" + SelfID, options)) {
                        if (options.callback_success)
                        options.callback_success(this);
                    }
                    else {
                        if (options.callback_failure)
                        options.callback_failure(this);
                    }
                });
                $(this).find('input').bind('focus keypress click',
                function() {
                    $("#" + SelfID).next('.' + options['error_class']).remove();
                    $("#" + SelfID).removeClass(options['error_field_class']);
                });
            }
            else {
                $(this).bind('blur',
                function() {
                    validate_field(this);
                });
                $(this).bind('focus keypress',
                function() {
                    $(this).next('.' + options['error_class']).fadeOut("fast",
                    function() {
                        $(this).remove();
                    });
                    $(this).removeClass(options['error_field_class']);
                });
            }
        }
        $(this).parents("form").submit(function() {
            if (validate_field('#' + SelfID))
            return true;
            else
            return false;
        });
        function validate_field(id) {
            var self = $(id).attr("id");
            var expression = 'function Validate(){' + options['expression'].replace(/VAL/g, '$(\'#' + self + '\').val()') + '} Validate()';
            var validation_state = eval(expression);
            if (!validation_state) {
                if ($(id).next('.' + options['error_class']).length == 0) {
                    $(id).after('<p class="' + options['error_class'] + '">' + options['message'] + '</p>');
                    $(id).addClass(options['error_field_class']);
                }
                if (ValidationErrors[FormID].join("|").search(id) == -1)
                ValidationErrors[FormID].push(id);
                return false;
            }
            else {
                for (var i = 0; i < ValidationErrors[FormID].length; i++) {
                    if (ValidationErrors[FormID][i] == id)
                    ValidationErrors[FormID].splice(i, 1);
                }
                return true;
            }
        }
    };
    $.fn.validated = function(callback) {
        $(this).each(function() {
            if (this.tagName == "FORM") {
                $(this).submit(function() {
                    if (ValidationErrors[$(this).attr("id")].length == 0)
                    callback();
                    return false;
                });
            }
        });
    };
})($);

//BASE CODE FOR CUSTOM FORM ELEMENTS
/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only things you may need to change in this file are the following
variables: checkboxHeight, radioHeight and selectWidth (lines 24, 25, 26)

The numbers you set for checkboxHeight and radioHeight should be one quarter
of the total height of the image want to use for checkboxes and radio
buttons. Both images should contain the four stages of both inputs stacked
on top of each other in this order: unchecked, unchecked-clicked, checked,
checked-clicked.

You may need to adjust your images a bit if there is a slight vertical
movement during the different stages of the button activation.

The value of selectWidth should be the width of your select list image.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "190";


/* No need to change anything after this */


document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5; } .disabled { opacity: 0.5; filter: alpha(opacity=50); }</style>');

var Custom = {
    init: function() {
        var inputs = document.getElementsByTagName("input"),
        span = Array(),
        textnode,
        option,
        active;
        for (a = 0; a < inputs.length; a++) {
            if ((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
                span[a] = document.createElement("span");
                span[a].className = inputs[a].type;

                if (inputs[a].checked == true) {
                    if (inputs[a].type == "checkbox") {
                        position = "0 -" + (checkboxHeight * 2) + "px";
                        span[a].style.backgroundPosition = position;
                    } else {
                        position = "0 -" + (radioHeight * 2) + "px";
                        span[a].style.backgroundPosition = position;
                    }
                }
                inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                inputs[a].onchange = Custom.clear;
                if (!inputs[a].getAttribute("disabled")) {
                    span[a].onmousedown = Custom.pushed;
                    span[a].onmouseup = Custom.check;
                } else {
                    span[a].className = span[a].className += " disabled";
                }
            }
        }
        inputs = document.getElementsByTagName("select");
        for (a = 0; a < inputs.length; a++) {
            if (inputs[a].className == "styled") {
                option = inputs[a].getElementsByTagName("option");
                active = option[0].childNodes[0].nodeValue;
                textnode = document.createTextNode(active);
                for (b = 0; b < option.length; b++) {
                    if (option[b].selected == true) {
                        textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
                    }
                }
                span[a] = document.createElement("span");
                span[a].className = "select";
                span[a].id = "select" + inputs[a].name;
                span[a].appendChild(textnode);
                inputs[a].parentNode.insertBefore(span[a], inputs[a]);
                if (!inputs[a].getAttribute("disabled")) {
                    inputs[a].onchange = Custom.choose;
                } else {
                    inputs[a].previousSibling.className = inputs[a].previousSibling.className += " disabled";
                }
            }
        }
        document.onmouseup = Custom.clear;
    },
    pushed: function() {
        element = this.nextSibling;
        if (element.checked == true && element.type == "checkbox") {
            this.style.backgroundPosition = "0 -" + checkboxHeight * 3 + "px";
        } else if (element.checked == true && element.type == "radio") {
            this.style.backgroundPosition = "0 -" + radioHeight * 3 + "px";
        } else if (element.checked != true && element.type == "checkbox") {
            this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
        } else {
            this.style.backgroundPosition = "0 -" + radioHeight + "px";
        }
    },
    check: function() {
        element = this.nextSibling;
        if (element.checked == true && element.type == "checkbox") {
            this.style.backgroundPosition = "0 0";
            element.checked = false;
        } else {
            if (element.type == "checkbox") {
                this.style.backgroundPosition = "0 -" + checkboxHeight * 2 + "px";
            } else {
                this.style.backgroundPosition = "0 -" + radioHeight * 2 + "px";
                group = this.nextSibling.name;
                inputs = document.getElementsByTagName("input");
                for (a = 0; a < inputs.length; a++) {
                    if (inputs[a].name == group && inputs[a] != this.nextSibling) {
                        inputs[a].previousSibling.style.backgroundPosition = "0 0";
                    }
                }
            }
            element.checked = true;
        }
    },
    clear: function() {
        inputs = document.getElementsByTagName("input");
        for (var b = 0; b < inputs.length; b++) {
            if (inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight * 2 + "px";
            } else if (inputs[b].type == "checkbox" && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 0";
            } else if (inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight * 2 + "px";
            } else if (inputs[b].type == "radio" && inputs[b].className == "styled") {
                inputs[b].previousSibling.style.backgroundPosition = "0 0";
            }
        }
    },
    choose: function() {
        option = this.getElementsByTagName("option");
        for (d = 0; d < option.length; d++) {
            if (option[d].selected == true) {
                document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
            }
        }
    }
}
window.onload = Custom.init;
