var imgsrc = null;
var custimg = null;
var year = "";
var category = "";
var winner = "";
var line1 = "";
var line2 = "";
var line3 = "";
var line4 = "";
var line5 = "";
var productname = "";
jQuery.noConflict();

function updatePreview() {
	var $ = jQuery;	
	$('#product-preview').html('<img width="400" src="/aias-awards/misc/renderimg.php?' +
		'year=' + encodeURIComponent(year) + '&' +
		'category=' + encodeURIComponent(category) + '&' +
		'winner=' + encodeURIComponent(winner) + '&' +
		'line1=' + encodeURIComponent(line1) + '&' +
		'line2=' + encodeURIComponent(line2) + '&' +
		'line3=' + encodeURIComponent(line3) + '&' +
		'line4=' + encodeURIComponent(line4) + '&' +
		'line5=' + encodeURIComponent(line5) + '&' +
		'productname=' + encodeURIComponent($('.product-name h1').html()) + '&' +
		'img=' + imgsrc + '" alt="" title="" id="product-gallery-image" class="image" />');
}

function updateFromQuery() {
	var query = window.location.search.substring(1);
	var vars = query.split('&');
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split('=');
		if (pair[0] == "line1") { line1 = unescape(pair[1]); }
		if (pair[0] == "line2") { line2 = unescape(pair[1]); }
		if (pair[0] == "line3") { line3 = unescape(pair[1]); }
		if (pair[0] == "line4") { line4 = unescape(pair[1]); }
		if (pair[0] == "line5") { line5 = unescape(pair[1]); }				
		if (pair[0] == "category") { category = unescape(pair[1]); }
		if (pair[0] == "winner") { winner = unescape(pair[1]); }		
		if (pair[0] == "year") { year = unescape(pair[1]); }
	}
}

jQuery(document).ready(function($) {
	// Add the customization elements we need
	$('#product-options-wrapper').before('<div id="product-preview"></div>').css({width: '200px', float: 'left'});
	$('#product-options-wrapper').after('<div style="clear: both"></div>');
	$('#product-preview').css({ width: '401px', float: 'right', position: 'relative', margin: '0 0 20px 0'});

	// Get the last picture
	custimg = $('.more-views img:last');
	imgsrc = $(custimg).attr('src');
	imgsrc = imgsrc.replace('thumbnail/56x', 'image');

	updateFromQuery();

	//Interactive Achievement Award
	try {
//		$("#select_5").attr("readonly", "readonly");
		$("#options_5_text").attr("readonly", "readonly");
		$("#options_4_text").attr("readonly", "readonly");
		$("#options_3_text").attr("readonly", "readonly");
		$("#options_2_text").attr("readonly", "readonly");
		$("#options_1_text").attr("readonly", "readonly");		
		
//		$('#select_5').change(function() { year = $('option:selected', this).text(); updatePreview();	});
		$('#options_5_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_4_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_3_text').blur(function()  {	winner = $(this).val(); updatePreview(); });
		$('#options_2_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_1_text').blur(function()  {	line2 = $(this).val(); updatePreview(); });
		
//		$('#select_5 option:contains(' + year + ')').attr("selected", true);		
		$('#options_5_text').val(year);
		$('#options_4_text').val(category);
		$('#options_3_text').val(winner);
		$('#options_2_text').val(line1);
		$('#options_1_text').val(line2);
	} catch(e) {}
	
	

	//Winner Framed Certificate + Stand
	try {
//		$("#select_9").attr("readonly", "readonly");
		$("#options_9_text").attr("readonly", "readonly");
		$("#options_10_text").attr("readonly", "readonly");
		$("#options_11_text").attr("readonly", "readonly");
		$("#options_24_text").attr("readonly", "readonly");
		$("#options_25_text").attr("readonly", "readonly");
		
		$('#options_9_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_10_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_11_text').blur(function()  {	winner = $(this).val(); updatePreview(); });
		$('#options_24_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_25_text').blur(function()  {	line2 = $(this).val(); updatePreview(); });
		
		$('#options_9_text').val(year);
		$('#options_10_text').val(category);
		$('#options_11_text').val(winner);
		$('#options_24_text').val(line1);
		$('#options_25_text').val(line2);
	} catch(e) {}

	//Winner Framed Certificate
	try {
		
//		$("#select_8").attr("readonly", "readonly");
		$("#options_8_text").attr("readonly", "readonly");
		$("#options_7_text").attr("readonly", "readonly");
		$("#options_6_text").attr("readonly", "readonly");
		$("#options_22_text").attr("readonly", "readonly");
		$("#options_23_text").attr("readonly", "readonly");
		
		
//		$('#select_8').change(function() { year = $('option:selected', this).text(); updatePreview();	});
		$('#options_8_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_7_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_6_text').blur(function()  {	winner = $(this).val(); updatePreview(); });
		$('#options_22_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_23_text').blur(function()  {	line2 = $(this).val(); updatePreview(); });
		
//		$('#select_8 option:contains(' + year + ')').attr("selected", true);		
		$('#options_8_text').val(year);
		$('#options_7_text').val(category);
		$('#options_6_text').val(winner);
		$('#options_22_text').val(line1);
		$('#options_23_text').val(line2);
	} catch(e) {}	
	
	//Finalist Framed Certificate
	try {
		
		$("#options_12_text").attr("readonly", "readonly");
		$("#options_13_text").attr("readonly", "readonly");
		$("#options_14_text").attr("readonly", "readonly");
		$("#options_15_text").attr("readonly", "readonly");
		$("#options_16_text").attr("readonly", "readonly");
		
		$('#options_12_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_13_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_14_text').blur(function()  {	winner = $(this).val(); updatePreview(); });
		$('#options_15_text').blur(function() {	line1 = $(this).val(); updatePreview(); });
		$('#options_16_text').blur(function()  {	line2 = $(this).val(); updatePreview(); });		
		
		$('#options_12_text').val(year);
		$('#options_13_text').val(category);
		$('#options_14_text').val(winner);
		$('#options_15_text').val(line1);
		$('#options_16_text').val(line2);		
	} catch(e) {}
	
	//Finalist Framed Certificate + Stand
	try {
		
		$("#options_17_text").attr("readonly", "readonly");
		$("#options_18_text").attr("readonly", "readonly");
		$("#options_19_text").attr("readonly", "readonly");
		$("#options_26_text").attr("readonly", "readonly");
		$("#options_27_text").attr("readonly", "readonly");
		
		$('#options_17_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_18_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_19_text').blur(function()  {	winner = $(this).val(); updatePreview(); });
		$('#options_26_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_27_text').blur(function()  {	line2 = $(this).val(); updatePreview(); });
		
		$('#options_17_text').val(year);
		$('#options_18_text').val(category);
		$('#options_19_text').val(winner);
		$('#options_26_text').val(line1);
		$('#options_27_text').val(line2);
	} catch(e) {}		

	$('#product-options-wrapper dl.last').prepend('<a href="/aias-awards/past-awards.php" rel="lyteframe" title="Press ESC to close" rev="width: 800px; height:400px; scrolling: yes;" id="search-past-awards">Click Here to Select Award and Search Past Awards</a>');
	$('#product-options-wrapper dl.last').append('<p style="color:red;font-size:smaller;margin-top:1em;">* Fields will populate for confirmation only.  Customization is not allowed.</p><style> .required {display:none;}</style>');
	$('.short-description .std').append('<p style="color:red;font-size:smaller;margin-top:1em;>Customization is not allowed.</p>');
	if (typeof(Lytebox) != "undefined") Lytebox.updateLyteboxItems();

	updatePreview();
	
		$(document).keydown(function(e) { if (e.charCode == 8 || e.keyCode == 8) return false; });
});

