/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/

var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};



var bVer = parseInt(navigator.appVersion)
if (bVer < 3 || (bVer < 4 && navigator.appName == "Microsoft Internet Explorer")) {
 bVer = "bad"
} else {
 bVer = "good"
}
 

function ChCl(RGBVal){
/*if (bVer == "good") {
 document.ColorGif.src = "/colorgifs/" + RGBVal + ".gif"
 }*/
 
 var RedOption = 0
 var GreenOption = 0
 var BlueOption = 0
 if (RGBVal.substring(0,2) == "FF")RedOption = "FF";
 if (RGBVal.substring(0,2) == "CC")RedOption = "CC";
 if (RGBVal.substring(0,2) == "99")RedOption = "99";
 if (RGBVal.substring(0,2) == "66")RedOption = "66";
 if (RGBVal.substring(0,2) == "33")RedOption = "33";
 if (RGBVal.substring(0,2) == "00")RedOption = "00";
 if (RGBVal.substring(2,4) == "FF")GreenOption = "FF";
 if (RGBVal.substring(2,4) == "CC")GreenOption = "CC";
 if (RGBVal.substring(2,4) == "99")GreenOption = "99";
 if (RGBVal.substring(2,4) == "66")GreenOption = "66";
 if (RGBVal.substring(2,4) == "33")GreenOption = "33";
 if (RGBVal.substring(2,4) == "00")GreenOption = "00";
 if (RGBVal.substring(4,6) == "FF")BlueOption = "FF";
 if (RGBVal.substring(4,6) == "CC")BlueOption = "CC";
 if (RGBVal.substring(4,6) == "99")BlueOption = "99";
 if (RGBVal.substring(4,6) == "66")BlueOption = "66";
 if (RGBVal.substring(4,6) == "33")BlueOption = "33";
 if (RGBVal.substring(4,6) == "00")BlueOption = "00";
 
 var bgcolor = RedOption+GreenOption+BlueOption;
 
  $('#color-box').css("display","block");
 $('#color-box').css("background","#"+bgcolor);

 
 var col_val1 = hex2num(RedOption);
 var col_val2 = hex2num(GreenOption);
 var col_val3 = hex2num(BlueOption);
  
 var RGB = col_val1 +","+ col_val2  +","+ col_val3;
 document.getElementById('rgbmix').value = RGB; 

}


var re_hexNum = /^[\da-f]+$/i;
function hex2num (s_hex) {
if (!re_hexNum.exec(s_hex))
return alert ('error');
eval("var n_num=0X" + s_hex);
return n_num;
}




function selectColor(id,color){
	
	document.getElementById('rgbmix').value = color;
	
	$('#color-box'+id).css("border","1px solid #FFFFFF");
	
	if(id == 1){
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	}
	if(id == 2){
		$('#color-box1').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	}
	if(id == 3){
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	}
	if(id == 4){
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	}
	if(id == 5){
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	}
	if(id == 6){
		
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	
	}
    
	if(id == 7){
		
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box8').css("border","none");
		$('#color-box9').css("border","none");
	
	}
	if(id == 8){
		
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box9').css("border","none");
	
	}
	if(id == 9){
		
		$('#color-box1').css("border","none");
		$('#color-box2').css("border","none");
		$('#color-box3').css("border","none");
		$('#color-box4').css("border","none");
		$('#color-box5').css("border","none");
		$('#color-box6').css("border","none");
		$('#color-box7').css("border","none");
		$('#color-box8').css("border","none");
	
	}
}

function release(id){
	alert(id);
	$('#'+id).css("border","none");
}


var divid = document.getElementById('display-img');

if(divid){
	$('#display-img').css("background","none");
}



        $(document).ready(function() {
            $(".dropdown img.flag").addClass("flagvisibility");

            $(".dropdown dt ").click(function() {
                $(".dropdown #idul ul").toggle();
            });
                        
            $(".dropdown #idul ul li ").click(function() {
                var text = $(this).html();
                $(".dropdown dt  span").html(text);
                $(".dropdown #idul ul").hide();
                $("#result").html("Selected value is: " + getSelectedValue("sample"));
            });
                        
            function getSelectedValue(id) {
                return $("#" + id).find("dt span.value").html();
            }

            $(document).bind('click', function(e) {
                var $clicked = $(e.target);
                if (! $clicked.parents().hasClass("dropdown"))
                    $(".dropdown #idul ul").hide();
            });


            $("#flagSwitcher").click(function() {
                $(".dropdown img.flag").toggleClass("flagvisibility");
            });
        });


