$(document).ready(function() {
    $('a[href=#top]').click(function(){
        $('html, body').animate({
            scrollTop:0
        }, 'slow');
        return false;
    });
    
    $('.msg').delay(5000).slideUp('fast');
    $("h1").each(function(){
        $(this).html($('<span>'+$(this).html()+'</span>'));
    });
    $('input[type=radio]').addClass('radio');
    $('input[type=checkbox]').addClass('checkbox');
});
$(function(){
    try{
    if($("#guest").length > 0){
        $("#guest").validate();
    }
    if($("#login").length > 0){
        $("#login").validate();
    }
    if($("form#forgotten").length > 0){
        $("form#forgotten").validate();
    }
    if($("#calculate_shipping").length > 0){
        $('#calculate_shipping').validate();
    }
    if($("form#password-form, form#address_2").length > 0){
        $("form#password-form, form#address_2").validate(
        {
            rules: {
                confirm: {
                    equalTo: "#password"
                }
            }
        }
        );
    }
    } catch(e){
        console.log(e);
    }
//    if($(".categories .cat_val ul").length > 0){
//        $('.categories .cat_val ul').masonry({
//
//            singleMode: false,
//            // Disables measuring the width of each floated element.
//            // Set to true if floated elements have the same width.
//            // default: false
//
//            columnWidth: 175,
//            // Width in pixels of 1 column of your grid.
//            // default: outer width of the first floated element.
//
//            //itemSelector: '.box:visible',
//            // Additional selector to specify which elements inside
//            // the wrapping element will be rearranged.
//            // Required for Infinite Scroll with window resizing.
//
//            resizeable: true,
//            // Binds a Masonry call to window resizes
//            // so layout appears fluid.
//            // default: true
//
//            animate: true,
//            // Animates layout rearrangements.
//            // default: false
//
//            animationOptions: {},
//            // A map of options for animation.
//
//            //appendedContent: $('.new_content'),
//            // Additional container element for appended content.
//            // Useful for Infinite Scroll integration.
//
//            saveOptions: true
//        // Masonry will use the options from previous Masonry
//        // calls by default, so you only have to enter in options once
//        // default: true
//
//        }, function() {}
//            // Optional callback.
//            // 'this' will refer to the applicable elements Masonry just rearranged
//
//            );
//    }
//    if($(".brands ul").length > 0){
//        $('.brands ul').masonry({
//
//            singleMode: true,
//            // Disables measuring the width of each floated element.
//            // Set to true if floated elements have the same width.
//            // default: false
//
//            columnWidth: 110,
//            // Width in pixels of 1 column of your grid.
//            // default: outer width of the first floated element.
//
//            //itemSelector: '.box:visible',
//            // Additional selector to specify which elements inside
//            // the wrapping element will be rearranged.
//            // Required for Infinite Scroll with window resizing.
//
//            resizeable: true,
//            // Binds a Masonry call to window resizes
//            // so layout appears fluid.
//            // default: true
//
//            animate: true,
//            // Animates layout rearrangements.
//            // default: false
//
//            animationOptions: {},
//            // A map of options for animation.
//
//            //appendedContent: $('.new_content'),
//            // Additional container element for appended content.
//            // Useful for Infinite Scroll integration.
//
//            saveOptions: true
//        // Masonry will use the options from previous Masonry
//        // calls by default, so you only have to enter in options once
//        // default: true
//
//        }, function() {}
//            // Optional callback.
//            // 'this' will refer to the applicable elements Masonry just rearranged
//
//            );
//
//    }
});
$('#content_search input').keydown(function(e) {
    if (e.keyCode == 13) {
        contentSearch();
    }
});
$('#search input').keydown(function(e) {
    if (e.keyCode == 13) {
        moduleSearch();
    }
});
$('.switcher').bind('click', function() {
    $(this).find('.option').slideDown('fast');
});
$('.switcher .options').bind('mouseleave', function() {
    alert('hover');
    $(this).find('.option').slideUp('fast');
});
function contentSearch() {
    url = 'http://mymexicandrugstore.org/catalog/view/javascript/jquery/index.php?route=product/search';

    var keyword = $('#keyword').attr('value');

    if (keyword) {
        url += '&keyword=' + encodeURIComponent(keyword);
    }

    var category_id = $('#category_id').attr('value');

    if (category_id) {
        url += '&category_id=' + encodeURIComponent(category_id);
    }

    if ($('#description').attr('checked')) {
        url += '&description=1';
    }

    location = url;
}
function getURLVar(urlVarName) {
    var urlHalves = String(document.location).toLowerCase().split('?');
    var urlVarValue = '';

    if (urlHalves[1]) {
        var urlVars = urlHalves[1].split('&');

        for (var i = 0; i <= (urlVars.length); i++) {
            if (urlVars[i]) {
                var urlVarPair = urlVars[i].split('=');

                if (urlVarPair[0] && urlVarPair[0] == urlVarName.toLowerCase()) {
                    urlVarValue = urlVarPair[1];
                }
            }
        }
    }

    return urlVarValue;
}
function moduleSearch() {
    url = 'http://mymexicandrugstore.org/catalog/view/javascript/jquery/index.php?route=product/search';

    var filter_keyword = $('#filter_keyword').attr('value')

    if (filter_keyword) {
        url += '&keyword=' + encodeURIComponent(filter_keyword);
    }

    var filter_category_id = $('#filter_category_id').attr('value');

    if (filter_category_id) {
        url += '&category_id=' + filter_category_id;
    }

    location = url;
}
function toggleMenu(obj,isOn) {
    var sClasses=obj.className;
    var aClass=sClasses.split(" ");
    if(!isOn)
        $('.'+aClass[1]).removeClass('over');
    else
        $('.'+aClass[1]).addClass('over');

}
function showAlert(message,title){
    $.pnotify({
        pnotify_opacity: .8,
        pnotify_title: (title==undefined?'Alert':title),
        pnotify_text: message
    });
}
function showError(message,title){
    $.pnotify({
        pnotify_type:'error',
        pnotify_opacity: .8,
        pnotify_title: (title==undefined?'Error':title),
        pnotify_text: message
    });
}
function createValidate(){
    $('form#create').validate(
    {
        rules: {
            firstname: {// compound rule
                required: true,
                minlength: 1,
                maxlength: 100
            },
            lastname: {// compound rule
                required: true,
                minlength: 1,
                maxlength: 100
            },
            address_1: {// compound rule
                required: true,
                minlength: 3,
                maxlength: 255
            },
            city: {// compound rule
                required: true,
                minlength: 3,
                maxlength: 255
            },
            password: {// compound rule
                required: true,
                minlength: 5,
                maxlength: 24
            },
            confirm: {
                equalTo: "#password"
            }
        }
        ,
        messages:{
            confirm: {
                equalTo: "Re-enter password should be same as password"
            }
        }
    }
    );
    $('#create').submit();
}

function bookmark(url, title) {
    if (window.sidebar) { // firefox
        window.sidebar.addPanel(title, url, "");
    } else if(window.opera && window.print) { // opera
        var elem = document.createElement('a');
        elem.setAttribute('href',url);
        elem.setAttribute('title',title);
        elem.setAttribute('rel','sidebar');
        elem.click();
    } else if(document.all) {// ie
        window.external.AddFavorite(url, title);
    }
}

function are_cookies_enabled()
{
    var cookieEnabled = (navigator.cookieEnabled) ? true : false;

    if (typeof navigator.cookieEnabled == "undefined" && !cookieEnabled)
    {
        document.cookie="testcookie";
        cookieEnabled = (document.cookie.indexOf("testcookie") != -1) ? true : false;
    }
    return (cookieEnabled);
}
