$(document).ready(function(){
	$('.lightbox a.zoom').lightBox();
	$('.lightboxMain a.zoom').lightBox();

	if( $('.dropdown_select').length > 0 ){
		$('.variation').hide();
		$('.variation:first').show();
	}
});

function force_case(){
	force_uppercase();
	force_altcase();
}

function force_uppercase(){
	$('.uppercase').each( function(){
		$(this).val( $(this).val().toUpperCase() );
	} );
}

function force_altcase(){
	$('.altcase').each( function(){
		$(this).val( $(this).val().toUpperCase() );
	} );
	if($('.altcase:first').length > 0){
		$('.altcase:first').val( $('.altcase:first').val().toLowerCase() );
	}

	if($('.altcase:last').length > 0){
		$('.altcase:last').val( $('.altcase:last').val().toLowerCase() );
	}
}

function swap_variation(current, ni){
	var prod_id = $('#' + current + ' option:selected').attr('title');
	
	if(prod_id == current){
		// Do nothing.
	} else{
		// Hide all the options and clear out all the quantities:
		$('.variation').hide();
		$('.variation input:visible').val('');

		// Reset the outgoing variation's select back to itself.
		$('#' + current).find('.dropdown_select').get(0).selectedIndex = ni;
		
		// Set the last_selected hidden input:
		$('#last_selected').val(prod_id);

		// Show it.
		$('#' + prod_id).show();
	}
}

function togglePreview(){
	$('#preview_button').toggle();
}

function lbPersonalization(){
	if( $(".lines input[value='']").length > 0 ){
		alert('Please check that you\'ve filled in the personalization form completely.');
		return;
	}

	var skus = new Array();
	var lbinput = new Array();
	var hpp = new String('');
	var rgb = new String('');
	var tmp = new String('');
	var colors = new Array('thread_color_id', 'ink_color_id');
	var thread_or_ink = new String('');

	// Get the relevant SKUs:
	if( $('.variation:visible .dropdown_select').length > 0 ){
		skus.push( $('.variation:visible option:selected').val() );
	} else{
		$('.rawsku').each( function(){
			var $this = $(this);
			skus.push( $this.text() );
		} );
	}

	// Get all the lines of input:
	$('.line input').each( function(){
		var $this = $(this);
		lbinput.push( $this.val() );
	} );

	// Get the RGB value of the thread or ink color selected:
	for(i=0; i<colors.length && rgb.length == 0; i++){
		rgb = $("select[name='per_opts[" + colors[i] + "]'] option:selected").attr('title');
		if(rgb == undefined){
			rgb = new String('');
		} else{
			//console.log(rgb);
			thread_or_ink = colors[i];
		}
	}

	// Parse the RGB string into an array:
	if(rgb != undefined){
		rgb = rgb.slice(4, -1).split(',');
	}

	// Create a hidden div in which to place the preview links:
	hpp += '<div id="hidden-personalization-preview">';
	for(i=0; i<skus.length; i++){
		hpp += '<a class="zoom" href="';
		hpp += 'http://s7d3.scene7.com/is/image/CathysConcepts/';
		hpp += $('#s7_image_id').text() + '-' + $("input[name='per_opts[style_id]']:checked").val() + '?';
		//console.log('Style ID: ' + $("input[name='per_opts[style_id]']:checked").val());
		hpp += 'layer=comp&';
		hpp += 'wid=500&';
		hpp += 'hei=500&';
		hpp += 'fmt=jpeg&';
		hpp += 'qlt=75,0&';
		hpp += 'op_sharpen=0&';
		hpp += 'resMode=bicub&';
		hpp += 'op_usm=1.0,1.0,8,0&';
		hpp += 'iccEmbed=0&';
		if(thread_or_ink == 'ink_color_id'){
			hpp += '$ink_color_id=' + $("select[name='per_opts[ink_color_id]'] option:selected").val() + '&';
			hpp += '$ink_color=' + $("select[name='per_opts[ink_color_id]'] option:selected").text().toLowerCase() + '&';
		}
		if(thread_or_ink == 'thread_color_id'){
			hpp += '$thread_color_id=' + $("select[name='per_opts[thread_color_id]'] option:selected").val() + '&';
			hpp += '$thread_color=' + $("select[name='per_opts[thread_color_id]'] option:selected").text().toLowerCase() + '&';

			// Transform input into image URLs:
			for(iter=0; iter<lbinput.length; iter++){
				var txt2img = new String('');
				if(lbinput[iter].length > 0){
					for(var letter = 0; letter < lbinput[iter].length; letter++){
						txt2img = '';
						txt2img += '$line_' + (iter + 1) + '_';
						txt2img += (letter + 1) + '=is{CathysConcepts%2F';
							txt2img += 'U' + lbinput[iter].charAt(letter).toUpperCase();
							txt2img += '_End_Scroll_';
							//txt2img += '_' + $("input[name='per_opts[style_id]']:checked").attr("title").replace(/ /gi, "_") + '_';
							txt2img += $("select[name='per_opts[thread_color_id]'] option:selected").text().replace(/ /gi, "_");
							txt2img += '_' + lbinput[iter].charAt(letter).toUpperCase();
							txt2img += '_';
							if(iter == 0 && letter == 0){
								txt2img += 'Left';
							} else if(iter == (lbinput.length - 1) && letter == (lbinput[iter].length - 1)){
								txt2img += 'Right';
							} else{
								txt2img += 'Middle';
							}
							txt2img += '_End_Scroll_';
							//txt2img += '_' + $("input[name='per_opts[style_id]']:checked").attr("title").replace(/ /gi, "_") + '_';
							txt2img += $("select[name='per_opts[thread_color_id]'] option:selected").text().replace(/ /gi, "_");
						txt2img += '%3Fscl%3D1}&';
						hpp += txt2img;
					}
				}
			}
		} else{
			for(iter=0; iter<lbinput.length; iter++){
				var line2char = new String('');
				if(lbinput[iter].length > 0){
					for(var letter = 0; letter < lbinput[iter].length; letter++){
						line2char += '$line_' + (iter + 1) + '_' + (letter + 1) + '=' + lbinput[iter].charAt(letter) + '&';
						hpp += line2char;
					}
				}
			}
		}
		if($("select[name='per_opts[gemstone_id]'] option:selected").text().length > 0){
			hpp += '$gemstone_id=' + $("select[name='per_opts[gemstone_id]'] option:selected").val() + '&';
			hpp += '$gemstone=' + $("select[name='per_opts[gemstone_id]'] option:selected").text().toLowerCase() + '&';
		}
		for(iter=0; iter<lbinput.length; iter++){
			if(lbinput[iter].length > 0){
				hpp += '$line_' + (iter + 1) + '=' + lbinput[iter].replace(/&/, '%26') + '&';
			}
		}
		if(rgb != undefined && rgb.length == 3){
			hpp += '$r=' + rgb[0] + '&';
			hpp += '$g=' + rgb[1] + '&';
			hpp += '$b=' + rgb[2];
		}
		hpp += '"></a>';
		//console.log(hpp);
	}
	hpp += '</div>';

	// Remove any previous hidden preview div, then add our new one:
	$('#hidden-personalization-preview').remove();
	$('body').append(hpp);

	// Activate lightbox on our hidden div and click the first link:
	$('#hidden-personalization-preview a').lightBox();
	$('#hidden-personalization-preview a:first').click();
}
