/**
 * @author Shawn Welch <shrimpwagon@yahoo.com>
 */
$(document).ready(function(){
    // Post - instead of get - JSON function
    $.postJSON = function(url, data, callback){
        $.post(url, data, callback, "json");
    };
    
    // Email cloaking
    $('a.base64-email').each(function(i, alink){
        var decoded_email = $.base64Decode($(alink).attr('email'));
        $(this).html(decoded_email);
        $(this).attr('href', 'mailto:' + decoded_email);
    });
    
    // Page sculpting
    $('.jquery-link').each(function(i, element){
        var $element = $(element);
        var href = $element.attr('link');
        var classes = $element.attr('class');
		var id = $element.attr('id');
        var innerHTML = $element.html();
        $element.replaceWith($('<a href="' + href + '" class="' + classes + '" id="' + id + '">' + innerHTML + '</a>'));
    });
	
	// Validation
	$.fn.validateForm = function(ajax_submit, options){
		var defaults = {
            onfocusout: false,
            onkeyup: false,
            showErrors: function(errorMap, errorList){
                if (errorList.length > 0) {
                    var message = "Please fix the following errors:\n\n"
                    for (key in errorList) {
                        // Add to message
                        message += '- ' + errorList[key].message + "\n";
                    }
                    alert(message);
                }
            },
            submitHandler: function(form){
                $(form).ajaxSubmit(ajax_submit);
            }
        }
		
		var settings = $.extend(true, defaults, options);
		
        $(this).validate(settings);
    }
	
	// Element blocking
	$.blockOptions = {
        message: '<img src="/themes/default/images/ajax-loader.gif" />',
        css: {
            backgroundColor: 'transparent',
            border: 'none'
        }
    }
    
    $.fn.blockElement = function(){
        $(this).block($.blockOptions)
    }
    
    // Simple accordion
    $('#faq').accordion({
        autoHeight: false,
        navigation: true,
        header: ".accordion-label",
        icons: false
    });
    
    // Popup tooltips
    $(".tooltip").tooltip({
        track: true,
        width: 200,
        showBody: '|',
        showURL: false,
        fade: 250,
        delay: 0
    });
    
    // Open SignMagic with a file for editing
    $.editSignSIF4 = function(filename){
        window.open('/sif4/sif4.php?filename=' + filename, null, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=' + screen.width + ',height=' + screen.height);
    }
    
    // Open SignMagic with some sign attributes
    $.startSIF4 = function(pf){
        window.open('/sif4/sif4.php?pf=' + pf, null, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=' + screen.width + ',height=' + screen.height);
    }
    
    // Logoff
    $.logoff = function(){
        $.eraseCookie('auto_email');
        window.location.reload(true);
    }
    
    // Open modal
	/*
    $.openModal = function(){
        // Clear box and show loading gif
        $('#colorbox-modal').html('<p align="center" style="margin-top: 200px"><img src="/themes/thesignchef/images/gallery_loading.gif" /></p>');
        
        $.fn.colorbox({
            href: '#colorbox-modal',
            width: "900px",
            height: "600px",
            speed: "100",
            inline: true,
            open: true
        });
    }
    */
    
    // Close modal
    $.closeModal = function(){
        $.fn.colorbox.close();
    }
    
    // Add to cart
    $('form.addtocart').each(function(){
        $(this).validate({
            showErrors: function(errorMap, errorList){
                if (errorList.length > 0) {
                    var message = "Please fix the following errors:\n\n"
                    for (key in errorList) {
                        // Add to message
                        message += '- ' + errorList[key].message + "\n";
                    }
                    alert(message);
                }
            },
            onfocusout: false,
            onkeyup: false
        })
    })
    
    $.addToCart = function(postfix_form_id){
        var $form = $('#addtocart-form-' + postfix_form_id);
        var valid = $form.valid();
        if (valid) {
            $.fn.colorbox({
                href: '/ajax/ajaxAddToCart.php?' + $form.formSerialize(),
                speed: "100",
                open: true,
                scrollbars: false
            }, function(){
                $('#addtocart-image').itemThumbnail({
                    "loading": '/themes/default/images/loading.gif',
                    "width": 200,
                    "height": 200
                });
            });
        }
    }
});
