var $k = jQuery.noConflict();
function showstyle(){
	var name="mysite_sheetswitch_idx";
	var value=document.getElementById('stylecolor').value
	//alert(value);
	var expires="7";
	deleteCookie(name);
	setCookie(name, value, expires)
	var col1=getCookie('col1');
	var col2=getCookie('col2');
	var col3=getCookie('col3');
	if(col1 && col1 && col1){
		
		
	}else{
		col1="dragBox1,dragBox2,dragBox8"; 
		col2="dragBox3,dragBox4,dragBox9";
		col3="dragBox5,dragBox6,dragBox7";
	}
	col1_array=col1.split(",")
	col2_array=col2.split(",")
	col3_array=col3.split(",")
	for(var i=1;i<8;i++){
		var check="check"+i;
		var dg_box="dragBox"+i;
		if(document.getElementById(check).checked){
			if(!Search_Array(col1_array,dg_box) && !Search_Array(col2_array,dg_box) && !Search_Array(col3_array,dg_box)){
				if(col1_array.length<3){
						col1=dg_box+","+col1	
						col1_array[col1_array.length]=dg_box;
					}else{
						if(col2_array.length<3){
							col2=dg_box+","+col2	
							col2_array[col2_array.length]=dg_box;
						}else{
							if(col3){
							  col3=dg_box+","+col3	
							  col3_array[col3_array.length]=dg_box;	
							}else{
							  col3=dg_box; 	
							  col3_array[0]=dg_box;	
							}
						}									
					}		
			}		
		}else{
			//alert(dg_box);
			var dg_box_rm=dg_box+",";
			if(Search_Array(col1_array,dg_box)){
				col1=col1.replace(dg_box_rm, "")
				if(col1_array[col1_array.length-1]==dg_box){
					dg_box_rm1=","+dg_box;
					col1=col1.replace(dg_box_rm1, "")
				}
				col1=col1.replace(dg_box, "");
				
				//if(col1_array.length==1){
					
				//}
			}
			if(Search_Array(col2_array,dg_box)){
				col2=col2.replace(dg_box_rm, "")	
				if(col2_array[col2_array.length-1]==dg_box){
					dg_box_rm1=","+dg_box;
					col2=col2.replace(dg_box_rm1, "")
				}
				/*if(col2_array.length==1){
					col2="";
				}*/
				col2=col2.replace(dg_box, "");
			}
			if(Search_Array(col3_array,dg_box)){
				//alert(col3);
				col3=col3.replace(dg_box_rm, "")	
				if(col3_array[col3_array.length-1]==dg_box){
					dg_box_rm1=","+dg_box;
					col3=col3.replace(dg_box_rm1, "")
				}
				/*if(col3_array.length==1){
					col3="";
				}*/
				col3=col3.replace(dg_box, "");
			}
			
			
			
				
		}		
	}
	/*alert(col1)
	alert(col2)
	alert(col3)*/
	setCookie("col1",col1,expires);
	setCookie("col2",col2,expires);
	setCookie("col3",col3,expires);	
	
	
	
	document.location.href=location.href;
}
$k("document").ready(function() {
	var colors = new Array;

	// Disable all .switch stylesheets and build array of colours
	$k(".switch[rel='stylesheet']").each(function() {
		$k(this).attr("disabled", "true");
		colors.push($k(this).css("color"));
	});
	
	
	$k(colors).each(function(index, el) {
		$k("#sheetswitch").append("<a class='swatch' style='background-color:" + el + ";'></a>");
		//$k("#stylecolor").value=el;
	});
	
	$k("#sheetswitch").append("");
	
	$k(".swatch").click(function() {
		$k(".swatch").removeClass("swatch_hi");
		$k(this).addClass("swatch_hi");
		var index = $k(".swatch").index(this);
		document.getElementById('stylecolor').value=index
		
		
		//document.getElementById('stylecolor').value=""
		
		/*$k(".switch[rel='stylesheet']").attr("disabled", "true");
		$k(".switch[rel='stylesheet']").eq(index).attr("disabled", "");
		$k.cookie('mysite_sheetswitch_idx', index, {expires: 7});*/
	});
	
	$k(".sheetswitch_next").click(function() {
		var selected = $k(".switch[rel='stylesheet']").filter(function () { return $k(this).attr("disabled") == false; });
		var current_idx = $k(".switch[rel='stylesheet']").index($k(selected));
		var length = $k(".switch[rel='stylesheet']").size();
		
		if (current_idx >= 0) {
			var next = current_idx + 1;
			if (next > (length - 1)) next = 0;
			
			$k(".switch[rel='stylesheet']").attr("disabled", "true");
			$k(".switch[rel='stylesheet']").eq(next).attr("disabled", "");
			
			$k(".swatch").removeClass("swatch_hi");
			$k(".swatch").eq(next).addClass("swatch_hi");
			
			$k.cookie('mysite_sheetswitch_idx', next, {expires: 7});
		}
		
		return false;
	});
	
	$k(".sheetswitch_prev").click(function() {
		var selected = $k(".switch[rel='stylesheet']").filter(function () { return $k(this).attr("disabled") == false; });
		var current_idx = $k(".switch[rel='stylesheet']").index($k(selected));
		var length = $k(".switch[rel='stylesheet']").size();
		
		if (current_idx >= 0) {
			var next = current_idx - 1;
			if (next == -1) next = (length - 1);
			
			$k(".switch[rel='stylesheet']").attr("disabled", "true");
			$k(".switch[rel='stylesheet']").eq(next).attr("disabled", "");
			
			$k(".swatch").removeClass("swatch_hi");
			$k(".swatch").eq(next).addClass("swatch_hi");
			
			$k.cookie('mysite_sheetswitch_idx', next, {expires: 7});
		}
		
		return false;
	});
	
	if (getCookie('mysite_sheetswitch_idx')!="") {
		var idx=getCookie('mysite_sheetswitch_idx');
		//alert(idx);
		//var idx = $k.cookie('mysite_sheetswitch_idx');
		$k(".switch[rel='stylesheet']").eq(idx).attr("disabled", "");
		$k(".swatch").eq(idx).addClass("swatch_hi");
	}
});

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}
function setCookie( name, value, expires, path, domain, secure )
{
	
	//alert(name+"="+value+"="+expires+"="+path+"="+domain+"="+secure)
// set time, it's in milliseconds

var domain=window.location.hostname;
var mydomin_array=domain.split(".");
var domaintype=mydomin_array[mydomin_array.length-1];

path="/";
if(domaintype=="com"){
	domain=".afterabc.com";
}else{	
	domain=".afterabc.co.kr";
}
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}
// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	
	var domain=window.location.hostname;
	var mydomin_array=domain.split(".");
	var domaintype=mydomin_array[mydomin_array.length-1];
	
	path="/";
	
	if(domaintype=="com"){
		domain=".afterabc.com";
	}else{	
		domain=".afterabc.co.kr";
	}
		
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
