/*!
 * (c) 2010 SysIQ, Inc. All rights reserved.
 *
 * This product is protected by United States laws,
 * international copyright treaties and all other applicable
 * national or international laws.
 *
 * This software may not, in whole or in part, be copied,
 * photocopied, translated, modified, or reduced to any
 * electronic medium or machine readable form
 * without the prior written consent of SysIQ, Inc.
 *
 * Filename: vec2.js
 * Author:   Alexander Goldobin
 */

var Vec2;
Vec2 = {
    x: function(v, value) {
        if (v.x != null) {
            if (value != null) {
                v.x = value;
            }
            else {
                return v.x;
            }
        }
        else if (v.left != null) {
            if (value != null) {
                v.left = value; 
            }
            else {
                return v.left;
            }
        }
        else if (v.width != null) {
            if (value != null) {
                v.width = value; 
            }
            else {
                return v.width;
            }
        }
        return 0;
    },
    y: function(v, value) {
        if (v.y != null) {
            if (value != null) {
                v.y = value;
            }
            else {
                return v.y;
            }
        }
        else if (v.top != null) {
            if (value != null) {
                v.top = value;
            }
            else {
                return v.top;
            }
        }
        else if (v.height != null) {
            if (value != null) {
                v.height = value;
            }
            else {
                return v.height;
            }
        }
        return 0;
    },
    xStyle: function(v, def) {
        if (v.x != null) {
            return "coords";
        }
        else if (v.left != null) {
            return "pos";
        }
        else if (v.width != null) {
            return "dims";
        }
        return def != null ? def : "none";
    },
    yStyle: function(v, def) {
        if (v.y != null) {
            return "coords";
        }
        else if (v.top != null) {
            return "pos";
        }
        else if (v.height != null) {
            return "dims";
        }
        return def != null ? def : "none";
    },
    create: function(x, y, mode) {
    	if (mode != null) {
	        if (mode.charAt(0) == 'p') {
	            return {
	                left: x,
	                top: y
	            };
	        }
	        else if (mode.charAt(0) == "d") {
	            return {
	                width: x,
	                height: y
	            };
	        }
    	}
    	
        return {
            x: x,
            y: y
        };
    },
    add: function(v1, v2, mode) {
        return Vec2.create(Vec2.x(v1) + Vec2.x(v2), Vec2.y(v1) + Vec2.y(v2), mode);
    },
    negate: function(v, mode) {
        return Vec2.create(- Vec2.x(v), - Vec2.y(v), mode);
    },
    sub: function(v1, v2, mode) {
        return Vec2.create(Vec2.x(v1) - Vec2.x(v2), Vec2.y(v1) - Vec2.y(v2), mode);
    },
    mul: function(v, scalar, mode) {
        return Vec2.create(Vec2.x(v) * scalar, Vec2.y(v) * scalar, mode);
    },
    copy: function(v1, v2) {
        Vec2.x(v1, Vec2.x(v2));
        Vec2.y(v1, Vec2.y(v2));
    },
    squareLength: function(v) {
        var vx = Vec2.x(v);
        var vy = Vec2.y(v);
        return vx * vx + vy * vy;
    },
    length: function(v) {
        return Math.sqrt(Vec2.squareLength(v));
    },
    positionStyle: function(v) {
        return Vec2.create(Vec2.x(v), Vec2.y(v), "pos");
    },
    dimensionsStyle: function(v) {
        return Vec2.create(Vec2.x(v), Vec2.y(v), "dims");
    },
    ceil: function(v) {
        return Vec2.create(Math.ceil(Vec2.x(v)), Math.ceil(Vec2.y(v)), Vec2.xStyle(v, "coords"));
    },
    floor: function(v) {
        return Vec2.create(Math.floor(Vec2.x(v)), Math.floor(Vec2.y(v)), Vec2.xStyle(v, "coords"));
    },
    round: function(v) {
        return Vec2.create(Math.round(Vec2.x(v)), Math.round(Vec2.y(v)), Vec2.xStyle(v, "coords"));
    },
    positionCss: function(v) {
        return {
            left: Vec2.x(v) + "px",
            top: Vec2.y(v) + "px"
        };
    },
    dimensionsCss: function(v) {
        return {
            width: Vec2.x(v) + "px",
            height: Vec2.y(v) + "px" 
        };
    }
};
