function initScreenShot()
{
    $('div.product_screenshot a.imagelink').lightBox({
    	imageLoading:			'img/lightbox/ico-loading.gif',
		imageBtnPrev:			'img/lightbox/btn-prev.gif',
		imageBtnNext:			'img/lightbox/btn-next.gif',
		imageBtnClose:			'img/lightbox/btn-close.gif',
		imageBlank:				'img/lightbox/blank.gif'
    });
}

function initReviewTab()
{
	nSelectedTab = (nFirstTabItemsCount == 0 && nSecondTabItemsCount > 0)?1:0;
	$('div.review_tab_wrap').tabs({ fxFade: true, fxSpeed: 'fast', selected:nSelectedTab});
}

function initTabs()
{
	$('ul.product_tabs li a').click(function(){
		/*$('.product_wrap').html('<div style="text-align:center; position:absolute; z-index:999; left:40%; top:30px; background:#fff; border:1px solid #369;"><img src="img/loading_best.gif" alt=""></div>' + 
			$('.product_wrap').html());*/
		$('.product_wrap').prepend('<div style="text-align:center; position:absolute; z-index:999; left:40%; top:30px; background:#fff; border:1px solid #369;"><img src="img/loading_best.gif" alt=""></div>');
		$('ul.product_tabs li.tab_act').removeClass('tab_act').addClass('tab');
		$('ul.product_tabs li a.act').removeClass('act');
		$(this).parents('li').removeClass('tab').addClass('tab_act');
		$(this).addClass('act');
		if (this.href.indexOf('screenshots') != -1) {
			var needInitScreenShot = true; 
		} else {
			var needInitScreenShot = false;
		}
		if (this.href.indexOf('videos') != -1) {
			var needInitVideo = true; 
		} else {
			var needInitVideo = false;
		}
		sURL = this.href;
		if($('#reseller_client_id').val()!=0) {
			sURL += '?reseller_client_id='+$('#reseller_client_id').val(); 
		}
		$.post(sURL, function(data){
			data = eval('(' + data + ')');
			$('.product_wrap').html(data.html);
			if (needInitScreenShot) {
				initScreenShot();
			}
			if (needInitVideo) {
				tb_init('a.thickbox:not(.thickbox_present)');
			}
		}, "json");
		return false;
	});
}

function showVariation(variation_id)
{
	$('ul.variation a').removeClass('variation_selected');
	$('ul.variation a.variation' + variation_id).addClass('variation_selected');
	$('select.variation').hide();
	$('select.variation' + variation_id).show();
	$('.product_license .maintenance').hide();
	$('.product_license .maintenance input[type="radio"]').removeAttr('checked');
	$('.product_license .maintenance' + variation_id + '_' + $('select.variation' + variation_id).val()).show();
	$('.product_license .maintenance' + variation_id + '_' + $('select.variation' + variation_id).val() + 
			' input[type="radio"]:first').attr('checked','checked');
	if($('select.variation' + variation_id).length > 0 && $('select.variation' + variation_id+' option').length > 0) {
		$('input#license_add_to_cart_btn').show();
		$('.specific_company').show();
	} else {
		$('input#license_add_to_cart_btn').hide();
		$('.specific_company').hide();
	}
	if($('select.variation' + variation_id+' option').length == 0) {
		$('select.variation' + variation_id).hide();	
	}
	$('input.variation_hidden').val(variation_id);
	$('.product_license select.variation'+variation_id).change();
	return false;
}

$(document).ready(function(){
    $('select.variation').change(function() {
        nVariationId = this.className.replace("variation variation", "");
		$('.product_license .maintenance').hide();
		$('.product_license .maintenance input[type="radio"]').removeAttr('checked');
		$('.product_license .maintenance' + nVariationId + '_' + $(this).val()).show();
		$('.product_license .maintenance' + nVariationId + '_' + $(this).val() + 
				' input[type="radio"]:first').attr('checked','checked');
				
		sId = '#purchase_type_'+nVariationId+'_'+$(this).val();
		sDivID = 'maintenance'+nVariationId+'_'+$(this).val();
		if($('#discounted_price_' + nVariationId + '_' + $(this).val()).val() == 0) {
		    //$('#purchase_options_area_'+nVariationId).hide();
			//$('#free_upgrade_area_'+nVariationId).show();
			//$('#free_license_id_'+nVariationId).val($(this).val());
			$('#license_add_to_cart_btn').hide();
			$('div#get_free_serial_btn_div_'+nVariationId).show();
			$('input#action_element').val('get_free_serials_pp');
			$('.maintenance' + nVariationId + '_' + $(this).val() + ' div#maintenance_options_list_' + nVariationId + '_' + $(this).val()).hide();
		} else {
		    if($('select.variation' + nVariationId).css('display') != 'none' &&
				typeof($('select.variation' + nVariationId).css('display')) != 'undefined') {
				$('#license_add_to_cart_btn').show();
			}
			$('div#get_free_serial_btn_div_'+nVariationId).hide();
			$('input#action_element').val('add_product');
			if($('select.variation' + nVariationId).css('display') != 'none' &&
				typeof($('select.variation' + nVariationId).css('display')) != 'undefined') {
				$('.maintenance' + nVariationId + '_' + $(this).val() + ' div#maintenance_options_list_' + nVariationId + '_' + $(this).val()).show();
			}
			
		}
		
		$('div.'+sDivID+' input[name="period"]').each(function () {
			if($(this).attr('checked')) {
				if(this.id.indexOf('_ext') != -1) {
					sSufix = this.id.replace('maint', '');
					sSufix = sSufix.replace('_1_ext', '');
					sSufix = sSufix.replace('_2_ext', '');
					$('#purchase_type_'+sSufix).val('maintenance');
					$('#purchase_type').val('maintenance');
				} else {
					sSufix = this.id.replace('maint', '');
					sSufix = sSufix.substr(0, sSufix.length-2);
					$('#purchase_type_'+sSufix).val('product');
					$('#purchase_type').val('product');
				}
			}
		});
		
		$('div.'+sDivID+' input[name="period"]').click(function () {
			if($(this).attr('checked')) {
				if(this.id.indexOf('_ext') != -1) {
					sSufix = this.id.replace('maint', '');
					sSufix = sSufix.replace('_1_ext', '');
					sSufix = sSufix.replace('_2_ext', '');
					$('#purchase_type_'+sSufix).val('maintenance');
					$('#purchase_type').val('maintenance');
				} else {
					sSufix = this.id.replace('maint', '');
					sSufix = sSufix.substr(0, sSufix.length-2);
					$('#purchase_type_'+sSufix).val('product');
					$('#purchase_type').val('product');
				}
			}
		});
		$('#purchase_type').val($(sId).val());
		$('#id_discount').val($("#id_discount_"+nVariationId+'_'+$(this).val()).val());
		$('#serial_id').val($("#serial_id_"+nVariationId+'_'+$(this).val()).val());
		$('#purchase_sub_type').val($("#purchase_sub_type_"+nVariationId+'_'+$(this).val()).val());
		//sId = "#purchase_sub_type_"+nVariationId+'_'+$(this).val();
		//alert(sId + '\n' + $(sId).val());
		
		/*if($('#license_free_'+nVariationId+'_'+$(this).val()).val() == 1) {
			$('input#license_add_to_cart_btn').hide();
		} else {
			if($('select.variation' + nVariationId).css('display') != 'none' &&
				typeof($('select.variation' + nVariationId).css('display')) != 'undefined') {
					$('input#license_add_to_cart_btn').show();
			}
		}*/
	}).change();
	$('.buy_now').click(function(){
		$('ul.product_tabs li a.pricing').click();
	});
	$('.video-link').click(function(){
		$('ul.product_tabs li a.videos').click();
	});
	CheckRadioButtons();	
});

function clearHiddenLicences()
{
	$('select.variation:hidden').remove();
}

function CheckRadioButtons() {
	$('div.purchase_options').each(function () {
		sIdStr = this.id.replace('purchase_options_', '');
		$('div#'+this.id+' input[type="radio"]:first').attr('checked','checked');
		$('div#'+this.id+' input[type="radio"]').each(function () {
			if($(this).attr('checked')) {
				sSufix = this.id.replace('maint', '');
				sSufix = sSufix.replace('_1_pl', '');
				sSufix = sSufix.replace('_2_pl', '');
				sSufix = sSufix.replace('_3_pl', '');
				$('#hidden_period_'+sSufix).val(this.value);
				$('#hidden_type_'+sSufix).val('product');
			}
		});
		sSelector = 'div#purchase_options_'+sIdStr+' input[name="period_'+sIdStr+'"]';
		$(sSelector).click(function () {
			if($(this).attr('checked')) {
				sSufix = this.id.replace('maint', '');
				sSufix = sSufix.replace('_1_pl', '');
				sSufix = sSufix.replace('_2_pl', '');
				sSufix = sSufix.replace('_3_pl', '');
				$('#hidden_period_'+sSufix).val(this.value);
				$('#hidden_type_'+sSufix).val('product');
			}
		});
	});	
}