$(function() {
	
	function Widget() 
	{
		//this.movie		= movie;
		var movie = 'widget';
		var class_name	= '.' + movie;
		var stage = {
			obj : $(class_name).find('object'),
			emb : $(class_name).find('embed')
		}
			stage.cur = $.browser.msie ? stage.obj : stage.emb 
			stage.setAttr = function (name, value) { 
				this.obj.attr(name, value)
				this.emb.attr(name, value)
			}
			stage.getAttr = function (name) { return this.cur.attr(name) }
		/*if ($.browser.msie)	
			 var stage = $(class_name + ' object');
		else 
			 var stage = $(class_name + ' embed');*/
		var picker    = $(class_name + ' .width_picker');
		var picker_id = movie	+ '_picker';
		var option100 = $(class_name + ' .option100');
		var code	  = $('#code');
		var swf		  = movie + '.swf';
		
		var ruler = $('#ruler');

		stage.cur.data('width', 	 stage.getAttr('width'));
		stage.cur.data('old_width',  stage.getAttr('width'));
		
		//==============================================||=======================
		/* For width */
		if(typeof(this.slider) === 'undefined') 				 
			this.slider = new MyCustomSlider({
				container: $(class_name+' .width_slider'),
				picker_id: picker_id,
				maxVal: 400,
				slideByStep: true,
				fn: {
					that: this,
					onChange: shift_width
				},
				defaultValue: 1
			});	
		function shift_width(width) {
			if (stage.cur.data('width') != 'elastic') {
				stage.setAttr('width', 250 + width);
				//$('h1').text(stage.attr('width'));
				stage.cur.data('width', 250 + width);
				stage.cur.data('old_width', 250 + width);
				
				ruler.text(250 + width);
				ruler.css('left', width - 10);
				
				if ($('#calendar').length)						//TODO: Find more clever sign to determine if width slider is over		
					ruler.css('top',  '-37px');
				else 
					ruler.css('top',  '20px');
				
			}	
		}
		
		/* For color */
		if ($('.widget_color_slider_container').length != 0) 
			if(typeof(this.colorslider) === 'undefined') 				 
			this.colorslider = new MyCustomSlider({
				container: $('.widget_color_slider'),
				picker_id: movie+'_color_picker',
				maxVal: 133,
				slideByStep: true,
				fn: {
					that: this,
					onChange: shift_widget_color
				},
				defaultValue: 1
			});	
		function shift_widget_color(hue) {							 
			var color = HSB2Hex(hue * 360/133, 100, 95);
			//get_swf(movie).set_head_color(color);
			Widget_color = color;
		}
		
		//=================================================||===================
		option100.click(function(){
			if ($(this).find('input:checked').length != 0) {
				stage.cur.data('width', 'elastic');
				stage.setAttr('width', '100%');
				picker.fadeOut('fast');
			}
			else {
				stage.cur.data('width', stage.cur.data('old_width'));
				stage.setAttr('width', stage.cur.data('width'));
				picker.fadeIn('fast');
			}
		});
		
		code.mouseup(function(e){
			$(this).select();
			e.preventDefault();
			e.stopPropagation();
			return false;
		});
		
		get_code();
		
		$('body').mouseup(function(){
			get_code();
			ruler.hide();
		});
		picker.mousedown(function(){
			ruler.show();
		});
		picker.mouseup(function(){
			get_code();
			ruler.hide();
		});
		option100.click(function(){
			get_code();
		});
		stage.cur.mouseup(function(){
			get_code();
		});

		function get_code() {
			var domain = $('#domain').attr('value'),
				paramAttr = 'params='+ domain +'|'+ Widget_color,
				ins = $('#widget')
			
			ins.find('param[name = "flashVars"]').attr('value', paramAttr)
			ins.find('embed').attr('FlashVars', paramAttr)
			
			code.text( ins.parent().html().toString() )
		}
		
	}
	
//    --- * Define * ---
	
	var widget = new Widget();

// switcher
/*	Switcher = function (container) {
		container.find('A').click(function(e) {
			if (! $(this).parent().hasClass('selected')) {
				var prevLink = container.find('.selected A');
				var newLink = $(this);
				
				var prevBlock = $('.' + prevLink.attr('mode') )
				var newBlock  = $('.' + newLink.attr('mode'));
				prevBlock.hide();
				newBlock.show();
				prevLink.parent().removeClass('selected');
				newLink.parent().addClass('selected');
			}
		});
		
	}
	
	var switcher = new Switcher($('#w-switcher'));*/
	
});

///////////////////////////////////////////////////////////////////

var Widget_color = '#EE0000';
function get_color() {
	return Widget_color;
}
// color convertor
function HSB2Hex (hue, saturation, brightness) {		
	var rgb = {};
	var h = Math.round(hue);
	var s = Math.round(saturation*255/100);
	var v = Math.round(brightness*255/100);
	if(s == 0) {
		r = g = b = v;
	} else {
		var t1 = v;
		var t2 = (255-s)*v/255;
		var t3 = (t1-t2)*(h%60)/60;
		if(h==360) h = 0;
		if(h<60) {r=t1;	b=t2; g=t2+t3}
		else if(h<120) {g=t1; b=t2;	r=t1-t3}
		else if(h<180) {g=t1; r=t2;	b=t2+t3}
		else if(h<240) {b=t1; r=t2;	g=t1-t3}
		else if(h<300) {b=t1; g=t2;	r=t2+t3}
		else if(h<360) {r=t1; g=t2;	b=t1-t3}
		else {r=0; g=0;	b=0}
	}
	r = Math.round(r), g = Math.round(g), b = Math.round(b)
	return '#'+((r << 16) + (g << 8) + b + 0xF000000).toString(16).substr(1, 8);
}
