/*
 * jQuery Plugin: DivCorners
 * http://www.roydukkey.com/divcorners
 *
 * Copyright (c) 2010 Rory Dueck
 *
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Date: 2010-04-19 (Mon, 19 Apr 2010)
 * Version: 1.5.3
 *
 * NOTE: Please report any improvements to roydukkey-at-gmail-dot-com.
 *       There are still many improvements that can me made to this
 *       script. Thanks to all in the open community.
 */
(function($){
var dc = {
	dcCreate: function(s,e) {
//--
// A: Set All Defaults
		var o = $.extend({
			imgPrefix: "/images/",
			fileType: ".gif",
			expand: 10,
			radius: 0,
			position: "inside",
			resize: "",
			exclude: ""
		},s);
//--
// Check Multiple Expand Values
// input: a and "a", "a,b", "a,b,c", "a,b,c,d"; output: [a,a,a,a], [a,b,a,b], [a,b,c,b], [a,b,c,d]
		o.e = o.expand.toString().replace(/[^0-9\. ]/g, "").split(" ");
		for (var i=0; i<4; i++)
			o.e[i] = !o.e[i] && o.e[i] !== 0 ? o.e[Math.max(i-2, 0)] : parseInt(o.e[i]);
//--
// Check Multiple Radii
// input: a and "a", "a,b", "a,b,c", "a,b,c,d"; output: [a,a,a,a], [a,b,a,b], [a,b,c,b], [a,b,c,d]
		o.r = o.radius.toString().replace(/[^0-9\. ]/g, "").split(" ");
		for (var i=0; i<4; i++)
			o.r[i] = !o.r[i] && o.r[i] !== 0 ? o.r[Math.max(i-2, 0)] : parseInt(o.r[i]) > o.e[i] ? parseInt(o.r[i])-o.e[i] : 0;
//--
// Create Array of Exclude String
		o.x = o.exclude.replace(/\s/g,"").split(",");
// A: End
//--
// B: Create DC for Each Object in Call
		return this.each(function() {
			var g = $(this),
				u = g.find(">.dcContent"),
				x,
				a = ["top","right","bottom","left"],
				v,
				y,
				b = ["left","right"],
				c,
				w = [];
			
			g.addClass("dCorner").css({
				height: g.height(),
				width: g.width()
			});
			u.css({ //Special for developers, shhh!
				height: u.height(),
				width: u.width()
			});
			o.resize != "" && g.find(o.resize).each(function() { //Apply dcResize to child images
				$(this).load(function() {
					$(this).parents(".dCorner").dcResize("auto")
				})
			});
			
			for(x in a) {
				v = $('<div class="dcItem dc'+a[x]+'">' + (
					 	$.inArray(a[x], o.x) == -1 ?
						'<img src="'+o.imgPrefix+a[x]+o.fileType+'" />' :
						""
					)	+ "</div>");

				if(x!=1 && x!=3) {
// top and bottom
					o.position == "inside" ?
						v.css("margin-left", o.e[3]+o.r[3])
							.width(g.innerWidth()-(o.r[3]+o.e[3]+o.r[1]+o.e[1]))
					:
						v.css(a[x], -o.e[x])
							.css("margin-left", o.r[3])
							.width(g.innerWidth()-(o.r[3]+o.r[1]))
					;
					v.height( o.e[x] )
						.css("display", "block");
					
// corners
					for(y in b) {
						c = $('<div class="dcItem dc'+a[x]+"-"+b[y]+'">' + (
							 	$.inArray(a[x]+"-"+b[y], o.x) == -1 ?
								'<img src="'+o.imgPrefix+a[x]+"-"+b[y]+o.fileType+'" />' :
								""
							)	+ "</div>");
						
						o.position != "inside" &&
							c.css(a[x], -o.e[x])
								.css(b[y], -o.e[y==1?1:3]);
						
						c.height( o.e[x] + o.r[x] )
							.width( o.e[ y==1?1:3 ] + o.r[ y==1?1:3 ] )
							.css("display", "block");
						
						$.inArray(a[x]+"-"+b[y], o.x) == -1 && f.p(c,o.fileType);
						
						w.push(c)
					}
				} else {
// right and left
					o.position == "inside" ?
						v.css("margin-top", o.e[0]+o.r[0])
							.height(g.innerHeight()-(o.r[0]+o.e[0]+o.r[2]+o.e[2]))
					:
						v.css( a[x], -o.e[x] )
							.css("margin-top", o.r[0])
							.height(g.innerHeight()-(o.r[0]+o.r[2]))
					;
					v.width( o.e[x] )
						.css("display", "block")
				}
				
				$.inArray(a[x], o.x) == -1 && f.p(v,o.fileType); // png filter
				w.push(v)
			}
			
			for(i in w) g.append(w[i]);
			
			f.w(g); // width fix
			e != false && g.parents(".dCorner").dcResize("auto")
		})
// B: End
//--
	},
	
	dcResize: function(e) {
//--
// C: Resize for Each Object in Call
		return this.each(function() {
			var g=$(this), c=g.find(">.dctop-left"), d=g.find(">.dctop-right");
			if( g.hasClass("dCorner") ) {
				g.css("zoom", "0");
				
				e == "auto" && g.css({height: "", width: ""});
				
				g.find(">.dctop,>.dcbottom")
					.width(
						g.innerWidth() - (
							c.width() + parseInt( c.css("left").replace("px") ) +
							d.width() + parseInt( d.css("right").replace("px") )
						)
					);
				g.find(">.dcleft,>.dcright")
					.height(
						g.innerHeight() - (
							c.height() + parseInt( c.css("top").replace("px") ) + 
							( d = g.find(">.dcbottom-left") ).height() + parseInt( d.css("bottom").replace("px") )
						)
					);
				
				f.w(g);
				e == "auto" && g.css({height: g.height(), width: g.width()});
				
				g.css("zoom", "1");
				e != false && g.parents(".dCorner").dcResize("auto")
			}
		})
// C: End
//--
	},
//--
// D: Resize for Each Object in Call	
	dcClear: function(e) {
		return this.each(function() {
			$(this).removeClass("dCorner").find(">.dcItem").remove();
			e!=false && $(this).parents(".dCorner").dcResize("auto")
		})
// D: End
//--
	}
};

//--
// E: jQuery Plugin Init and Private Functions
$.each(dc, function(i) {
	$.fn[i] = this
});

// Private Functions
var f = {

	// png Fix
	p: function(g,n) {
		if($.browser.msie && n == ".png") {
			var i=(g = $(g)).find("img");
			g.css("filter", "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + i.attr("src") + "',sizingMethod='scale')");
			i.css("filter", "alpha(opacity=0)")
		}
	},
	
	// Width Fix
	w: function(c) {
		// this is use to compensated for ie6's miscalculation
		// of percent width and actual width
		if($.browser.version < 7.0 && $.browser.msie) {
			c.find(">.dctop-right,>.dcright,>.dcbottom-right")
				.css("margin-right", c.innerWidth()%2 != 0 ? "-1px" : "0px");
			c.find(">.dcbottom-left,>.dcbottom,>.dcbottom-right")
				.css("margin-bottom", c.innerHeight()%2 != 0 ? "-1px" : "0px")
		}
	}
	
}
	
})(jQuery);
