/**
 * @copyright  Copyright (c) 2007 August Ash Inc. (http://www.augustash.com)
 * @version    $Id$
 */

/**
 * Prepare Links - using jQuery
 * 
 * Checks the document, when ready, for all link nodes with a class
 * name "external" and opens them in a new window when clicked.
 */
$(document).ready(function(){
	$("a").filter(".external").click(function (){
		var NewWindow = new OpenWindow($(this).attr("href"));
		return NewWindow.open();
	})
	.end()
	.filter(".fplayer").click(function (){
		var NewWindow = new OpenWindow($(this).attr("href"));
		NewWindow.setFeatures("status=0,height=330,width=330,resizable=0");
		return NewWindow.open();
	});
});

/**
 * OpenWindow Class
 * 
 * Creates an OpenWindow object that allows you to define the URL, 
 * window name, and features for firing a pop-up window.
 * 
 * @param {String} href
 */
function OpenWindow(href)
{
	// Set default values
	var _href     = href;
	var _name     = "external";
	var _features = "";
	
	function __construct() {
		// Define methods		
		this.getHref     = function() { return _href; }
		this.setHref     = function(href) { _href = href; }
		this.getName     = function() { return _name; }
		this.setName     = function(name) { _href = name; }
		this.getFeatures = function() { return _features; }
		this.setFeatures = function(features) { _features = features; }
		
		this.open = function() {
			window.open(_href, _name, _features);
			return false;
		}
	};
	
	return new __construct();
}

/**
 * Simply URL redirection function
 *
 * @param string url
 */
function setLocation(url) {
    window.location.href = url;
}

/**
 * Force the form to only be submitted once.
 * The onsubmit action will also only be allowed to run once.
 * 
 * <form .. onsubmit="return submit_once(this);">
 * 
 * @param {HTML Form Object} form
 * @return bool
 */
function submit_once(form) {
    form.onsubmit = function() {
        return false; 
    };
    return true;
}

/**
 * Fires off an AJAX request to update order's print status.
 * If successful, opens browser's print dialog
 * @param {int} orderId
 */
function printOrder(orderId)
{
	$.ajax({
		type: "GET",
		url: "/admin/orders/print/id/" + orderId,
		dataType: "json",
		success: function(data){
			if (data.status == 'success') {
                if (data.new_status) {
                    $('#status_id').val(data.new_status);
                }
                // send request to print
                $("#frmOrderPrint").submit();
			}
		},
		error: function(){
			alert('Order could not be marked as printed');
		}
	})
	
	return false;
}

/**
 * Check all 'checkbox' elements or un-check
 * @param {Boolean} checkAll
 */
function selectOrders(checkAll)
{
	var checks = $('input:checkbox', document.forms[1]);
	for (i = 0; i < checks.length; i++) {
		if (checkAll) {
			checks[i].checked = true;
		} else {
			checks[i].checked = false;
		}
	}
	return false;
}

/*
*
*/	

