// JavaScript Document

var ibcClass= Class.create({
	
	initialize: function(){
		this.wrapper = $('ibc');
		this.colour_wrapper = $('ibc_colours');
		this.text_el = $('ibc_text').down('input');
		this.tees_grid = $('ibc_tee_grid');
		this.loadColourPalettes();
		
		this.randomiseColours();
		this.updateText($F(this.text_el));
		
	},
	
	/* figure out where the colour belongs on the "map" */
	getColourOrder: function(colour){
		this.colours.each(function(c,i){
			if(colour == c)	colour_order = i;
		});
		
		return colour_order;
	},
	
	/* Get colour positions on the grid */
	getPositionY: function(colour_order)
	{
		row = Math.floor(colour_order / 10);
		return '-'+(row * this.shirt.height) + 'px';
	},
	getPositionX: function(colour_order){
		column = (colour_order % 10);
		return '-'+(column * this.shirt.width) + 'px'
	},
	
	/* Shirt Animation */
	gotoShirt: function(colour)
	{
		var colour_order = this.getColourOrder(colour);
		
		$$('.ibc_print_colour_grid').each(function(cg){ cg.up('div').setOpacity(0); });
		$('ibc_text_container').setOpacity(0);

		// reposition grid
		// this.tees_grid.morph({top: this.getPositionY(colour_order)}, {transition:Effect.Transitions.spring});
		var foo = '-'+(940*colour_order)+'px';
		this.tees_grid.morph({left: foo}, {transition: Effect.Transitions.spring
});
		
		setTimeout(function(){
			$$('.ibc_print_colour_grid').each(function(cg){
				cg.up('div').setStyle({paddingTop:'20px'});
				cg.up('div').morph({paddingTop:'0px'}, {duration:0.4});
				cg.up('div').appear({from:0, to:1, duration:0.4});
				$('ibc_text_container').appear({from:0, to:1, duration:0.4});
			});
		}, 790);
	},
	
	/* Loads colour pallettes */
	loadColourPalettes: function(){
		
		this.colour_wrapper.select('dl').each(function(dl){
			var field = dl.down('input');
			var ul = new Element('ul',{className:'clearfix'});
			dl.down('dd').update('').insert({top: ul });
			
			this.colours.each(function(c){
				ul.insert({bottom:
					new Element('li',{className:c}).setStyle({backgroundColor:'#'+c}).observe('mouseenter', function(){
						/* Mouse Entering Event */
						
						this.addClassName('hovering');
						
					}).observe('mouseleave', function(){
						/* Mouse Leaving Event */
						
						this.removeClassName('hovering');
						
					}).observe('click', function(){
						/* Click Event */						
						ibc.selectColour(dl.identify(), c);
					})
				});
			});
		}.bind(this));
	
	},
	
	/* Randomises colours */
	randomiseColours: function(){
		this.colour_wrapper.select('dl').each(function(dl){
			rand = Math.floor(Math.random()*this.colours.length);
			this.selectColour(dl.identify(), dl.select('li')[rand].className);
		}.bind(this));
	},
	
	/* Selects Colour */
	selectColour: function(colour_type, hex)
	{
		var formhex;
		var colour_order = this.getColourOrder(hex);
	
		if (hex =="58351c") formhex = '57';
		if (hex =="ca3238") formhex = '58';
		if (hex =="f7e267") formhex = '59';
		if (hex =="d4bacc") formhex = '60';
		if (hex =="7e4763") formhex = '61';
		if (hex =="2e4498") formhex = '62';
		if (hex =="cedbcf") formhex = '63';
		if (hex =="deeed1") formhex = '64';
		if (hex =="56706e") formhex = '65';
		if (hex =="a5a4a9") formhex = '66';
		if (hex =="fcf4dc") formhex = '67';
		if (hex =="e34761") formhex = '68';
		if (hex =="f4bf2e") formhex = '69';
		if (hex =="f0c8e1") formhex = '70';
		if (hex =="9d4e7a") formhex = '71';
		if (hex =="183d7f") formhex = '72';
		if (hex =="acc7c7") formhex = '73';
		if (hex =="defeff") formhex = '74';
		if (hex =="498f58") formhex = '75';
		if (hex =="bfc8cc") formhex = '76';
		if (hex =="ffffff") formhex = '77';
		if (hex =="bd295a") formhex = '78';
		if (hex =="e06723") formhex = '79';
		if (hex =="faeaed") formhex = '80';
		if (hex =="e050a1") formhex = '81';
		if (hex =="0f255d") formhex = '82';
		if (hex =="9fd7e1") formhex = '83';
		if (hex =="bfdaed") formhex = '84';
		if (hex =="80b668") formhex = '85';
		if (hex =="424242") formhex = '86';
		if (hex =="000000") formhex = '87';
		if (hex =="9a2545") formhex = '88';
		if (hex =="e03f2e") formhex = '89';
		if (hex =="fffdae") formhex = '90';
		if (hex =="f0b2ad") formhex = '91';
		if (hex =="3a2e8c") formhex = '92';
		if (hex =="4291ac") formhex = '93';
		if (hex =="91beeb") formhex = '94';
		if (hex =="9cd7c9") formhex = '95';
		if (hex =="626518") formhex = '96';

		 
		
		$(colour_type).down('input').value = formhex;
		$(colour_type).select('li').each(function(li){

			if(li.hasClassName(hex))
				li.addClassName('selected');
			else
				li.removeClassName('selected');
		});
		
		// shirt colour animation
		switch(colour_type)
		{
			case 'ibc_shirt_colour': this.gotoShirt(hex); break;
			default:
				$('ibc_'+colour_type+'_grid').setStyle({
					top: this.getPositionY(colour_order),
					left: this.getPositionX(colour_order)
				});
				break;
				
		}
		if(colour_type == 'ibc_shirt_colour') this.gotoShirt(hex);
	},
	
	selectSize: function(size)
	{
		$('ibc_buttons').down('input').value = size;
		$('ibc_buttons').select("li").each(function(li){
			if(li.hasClassName(size))
				li.addClassName('selected');
			else
				li.removeClassName('selected');
		});
		
		return false;
	},
	
	/* Triggers when the text is put into focus */
	textFocused: function(){
		
		if(!this.text_el.hasClassName('initiated'))
		{
			this.text_el.addClassName('initiated');
			this.text_el.value = '';
			this.updateText('');	
		}
	},
	
	updateText: function(text)
	{

		if(text.length > 9)
			alert('Maximum of 9 letters');
		else
		{
			$('ibc_text_container').down('span').update(text);
			$('ibc_text_container').down('input').value = text;	
		}
	},	
	
	validate: function(formname){
		var returnval=false;
        var msg="";

        if(formname.size.value=="") {
            msg+="Please choose a t-shirt size \n";
			
		}
        if(msg=="")
        {
            returnval=true;
			
            return returnval;
        }
        else
        {
            alert(msg);
            return returnval;
        } 
	},
	
	submit_form: function(formname)
    {
        document.forms["cart_quantity"].submit();
        return true;
		
		/* if(ibc.validate(formname))
        {
			document.forms["cart_quantity"].submit();
            //document.formname.submit();
			return true;
			
        }
        else
        {
            
        }
		*/
    },
	
	/* Constants */
	colours: [
		"58351c", "ca3238", "f7e267", "d4bacc", "7e4763", "2e4498", "cedbcf", "deeed1", "56706e", "a5a4a9", 
		"fcf4dc", "e34761", "f4bf2e", "f0c8e1", "9d4e7a", "183d7f", "acc7c7", "defeff", "498f58", "bfc8cc", 
		"ffffff", "bd295a", "e06723", "faeaed", "e050a1", "0f255d", "9fd7e1", "bfdaed", "80b668", "424242",	
		"000000", "9a2545", "e03f2e", "fffdae", "f0b2ad", "3a2e8c", "4291ac", "91beeb", "9cd7c9", "626518"	
	],
	sizes: ["xs", "s", "m", "l"],
	shirt: {width:940,height:404}
	
});
