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="350" src="/gta-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: '350px', 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();
	
	//Golden Trailer Award
	try {
		$("#options_12_text").attr("readonly", "readonly");
		$("#options_13_text").attr("readonly", "readonly");
		$("#options_14_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_2_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_28_text').blur(function() {	line2 = $(this).val(); updatePreview(); });
		$('#options_29_text').blur(function() {	line3 = $(this).val(); updatePreview(); });
		$('#options_30_text').blur(function() {	line4 = $(this).val(); updatePreview(); });
		$('#options_31_text').blur(function() {	line5 = $(this).val(); updatePreview(); });

		$('#options_12_text').val(year);
		$('#options_13_text').val(category);
		$('#options_14_text').val(winner);
		$('#options_2_text').val(line1);
		$('#options_28_text').val(line2);
		$('#options_29_text').val(line3);
		$('#options_30_text').val(line4);
		$('#options_31_text').val(line5);
	} catch(e) {}
	
	//Official Selection Silver Award
	try {		
		$("#options_21_text").attr("readonly", "readonly");
		$("#options_22_text").attr("readonly", "readonly");
		$("#options_23_text").attr("readonly", "readonly");
		
		$('#options_21_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_22_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_23_text').blur(function() {	winner= $(this).val(); updatePreview(); });
		$('#options_5_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_32_text').blur(function() {	line2 = $(this).val(); updatePreview(); });
		$('#options_33_text').blur(function() {	line3 = $(this).val(); updatePreview(); });
		$('#options_34_text').blur(function() {	line4 = $(this).val(); updatePreview(); });
		$('#options_35_text').blur(function() {	line5 = $(this).val(); updatePreview(); });

		$('#options_21_text').val(year);
		$('#options_22_text').val(category);
		$('#options_23_text').val(winner);
		$('#options_5_text').val(line1);
		$('#options_32_text').val(line2);
		$('#options_33_text').val(line3);
		$('#options_34_text').val(line4);
		$('#options_35_text').val(line5);
	} catch(e) {}

	//Gold Team Award
	try {
		$("#options_9_text").attr("readonly", "readonly");
		$("#options_8_text").attr("readonly", "readonly");
		$("#options_7_text").attr("readonly", "readonly");
		
		$('#options_9_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_8_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_7_text').blur(function() {	winner= $(this).val(); updatePreview(); });
		$('#options_1_text').blur(function() {	line1 = $(this).val(); updatePreview(); });

		$('#options_9_text').val(year);
		$('#options_8_text').val(category);
		$('#options_7_text').val(winner);
		$('#options_1_text').val(line1);
	} catch(e) {}	
	
	//Official Selection Silver Team Award
	try {
		$("#options_24_text").attr("readonly", "readonly");
		$("#options_25_text").attr("readonly", "readonly");
		$("#options_26_text").attr("readonly", "readonly");
		
		$('#options_24_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_25_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_26_text').blur(function() {	winner= $(this).val(); updatePreview(); });
		$('#options_6_text').blur(function() {	line1 = $(this).val(); updatePreview(); });

		$('#options_24_text').val(year);
		$('#options_25_text').val(category);
		$('#options_26_text').val(winner);
		$('#options_6_text').val(line1);
	} catch(e) {}	
	
	//GTA Plaque
	try {
		$("#options_15_text").attr("readonly", "readonly");
		$("#options_16_text").attr("readonly", "readonly");
		$("#options_17_text").attr("readonly", "readonly");
				
		$('#options_15_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_16_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_17_text').blur(function() {	winner= $(this).val(); updatePreview(); });
		$('#options_3_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_48_text').blur(function() {	line2 = $(this).val(); updatePreview(); });
		$('#options_49_text').blur(function() {	line3 = $(this).val(); updatePreview(); });
		$('#options_50_text').blur(function() {	line4 = $(this).val(); updatePreview(); });
		$('#options_51_text').blur(function() {	line5 = $(this).val(); updatePreview(); });

		$('#options_15_text').val(year);
		$('#options_16_text').val(category);
		$('#options_17_text').val(winner);
		$('#options_3_text').val(line1);
		$('#options_48_text').val(line2);
		$('#options_49_text').val(line3);
		$('#options_50_text').val(line4);
		$('#options_51_text').val(line5);
	} catch(e) {}

	//GTA Poster Awards
	try {
		$("#options_18_text").attr("readonly", "readonly");
		$("#options_19_text").attr("readonly", "readonly");
		$("#options_20_text").attr("readonly", "readonly");
				
		$('#options_18_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_19_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_20_text').blur(function() {	winner= $(this).val(); updatePreview(); });
		$('#options_4_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_36_text').blur(function() {	line2 = $(this).val(); updatePreview(); });
		$('#options_37_text').blur(function() {	line3 = $(this).val(); updatePreview(); });
		$('#options_38_text').blur(function() {	line4 = $(this).val(); updatePreview(); });
		$('#options_39_text').blur(function() {	line5 = $(this).val(); updatePreview(); });

		$('#options_18_text').val(year);
		$('#options_19_text').val(category);
		$('#options_20_text').val(winner);
		$('#options_4_text').val(line1);
		$('#options_36_text').val(line2);
		$('#options_37_text').val(line3);
		$('#options_38_text').val(line4);
		$('#options_39_text').val(line5);
	} catch(e) {}

	//Official Selection Silver Poster
	try {
		$("#options_41_text").attr("readonly", "readonly");
		$("#options_42_text").attr("readonly", "readonly");
		$("#options_43_text").attr("readonly", "readonly");
				
		$('#options_41_text').blur(function() {	year = $(this).val(); updatePreview(); });
		$('#options_42_text').blur(function() {	category = $(this).val(); updatePreview(); });
		$('#options_43_text').blur(function() {	winner= $(this).val(); updatePreview(); });
		$('#options_40_text').blur(function()  {	line1 = $(this).val(); updatePreview(); });
		$('#options_44_text').blur(function() {	line2 = $(this).val(); updatePreview(); });
		$('#options_45_text').blur(function() {	line3 = $(this).val(); updatePreview(); });
		$('#options_46_text').blur(function() {	line4 = $(this).val(); updatePreview(); });
		$('#options_47_text').blur(function() {	line5 = $(this).val(); updatePreview(); });

		$('#options_41_text').val(year);
		$('#options_42_text').val(category);
		$('#options_43_text').val(winner);
		$('#options_40_text').val(line1);
		$('#options_44_text').val(line2);
		$('#options_45_text').val(line3);
		$('#options_46_text').val(line4);
		$('#options_47_text').val(line5);
	} catch(e) {}

	$('#product-options-wrapper dl.last').prepend('<a href="/gta-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>');
	if (typeof(Lytebox) != "undefined") Lytebox.updateLyteboxItems();

	updatePreview();
	
		$(document).keydown(function(e) { if (e.charCode == 8 || e.keyCode == 8) return false; });
});

