var regex = "bryle-([\\w\\-]+)-model-([\\w\\-]+)";
var re = new RegExp(regex);

$(document).ready(function() {
	$('#loading').hide();

	// disable product rating
	var productId = $('input[name=productId]').val();
	var value = getCookie('productRating');
	if(value == productId) {
		$('#itemRating').hide();
	}

	$('input[name=authorName]').click(function() {
		$('input[name=authorName]').val('');
	});

	$('input[name=submit]').click(function() {
		if($('input[name=authorName]').val() == ''  ||  $('textarea[name=authorComment]').val() == '') {
			alert('Prosím vyplťe jméno a text příspěvku.');
			return false;
		}
	});

	$('div#itemColorVariation a').click(function() {

		$('#loading').show();

		var url = $(this).attr('href').split('?');
		var skuUrl = url[1];
		url = url[0].replace('.html', '');
	  	var result = re.exec(url);
	  	var brandUrl = result[1];
	  	var productUrl = result[2];

		$.getJSON(baseUrl + 'product/ajax-get-sku/',
			{
				skuUrl: skuUrl,
				productUrl: productUrl,
				brandUrl: brandUrl
			}, function(json) {
				onGetSku(json);
			}
		);

		return false;
	});

	// auto submit star rating
	$('.star').rating({ 
	  callback: function(value, link){
		var productId = $('input[name=productId]').val();
		$.getJSON(baseUrl + 'product/ajax-rating/',
			{
				productId: productId,
				rating: value
			}, function(json) {
				onProductRating(json);
			}
		);
	  } 
	});

});


function onProductRating(json)
{
	$('#itemRating').slideToggle('normal');
	updateProductRating();
	var productId = $('input[name=productId]').val();
	setCookie('productRating', productId);
}


function updateProductRating()
{
	var productId = $('input[name=productId]').val();
	$.getJSON(baseUrl + 'product/ajax-get-rating/',
		{
			productId: productId
		}, function(json) {
			$('span.rating').addClass('rating'+json.rating);
		}
	);
}


function onGetSku(json)
{
	var productId = $('input[name=productId]').val();
        var s;

	$('div#itemColorVariation a').removeAttr('style');

	$('#delivery').text('');
	$('#size').text('');
	$('#polarized').text(((json.polarized == 1) ? 'Ano' : 'Ne'));
	$('#frame_color').text(json.frame_color);


        $('#lens_color').text('');
        if(json.lenses.length > 0) {
            for(i = 0; i < json.lenses.length ; i++) {
                $('#lens_color').append(json.lenses[i].name);
                s = '';
                if(json.lenses[i].description.length > 0) {
                    $('#lens_color').append(' <a href="#" class="tooltipTrigger helper">[?]</a>');
                    s = '<div class="tooltip tooltipSmall"><h3>' + json.lenses[i].name + '</h3><p>' + json.lenses[i].description + '</p>';
                    if(json.lenses[i].light_transmission.length > 0) {
                        s = s + '<p>Světelná propustnost: ' + json.lenses[i].light_transmission + '</p>';
                    }
                    if(json.lenses[i].protection_index != null) {
                        s = s + '<p>Ochranný index: ' + json.lenses[i].protection_index + '</p>';
                    }
                    if(json.lenses[i].light_conditions.length > 0) {
                        s = s + '<p>Světelné podmínky: ' + json.lenses[i].light_conditions + '</p>';
                    }
                    s = s + '</div>';
                }
                $('#lens_color').append(s);
                $('#lens_color').append('<br />');
            }
            startTooltip();
        }

	if (json.delivery == json.default_delivery_text) {
		var link = '<a rel="colorbox-question-'+productId+'" title="Dotaz k produktu ' + json.product_name + '" class="contact" href="dotaz-k-produktu-' + productId + '.html?variant=' + json.url + '">' + json.delivery + '</a>';
		$('#delivery').html(link);
	} else {
		$('#delivery').text(json.delivery);
	}
	
	$('#variant').text(json.pn_orig);
	$('#size').text(json.size);
	$('#price').text(json.price);
	$('#price_common').text(json.price_common);
	$('input[name=variant]').val(json.url);
	$('div#itemColorVariation a').removeClass('colorActive');
	$('div#itemColorVariation a[href*='+json.url+']').addClass('colorActive');

	// show special properties
	var specialProperties = json.special_properties;
	$('div#itemProperties ul').remove();

	if(specialProperties.length > 0) {
		$('div#itemProperties').append('<ul></ul>');
		for(i = 0; i < specialProperties.length ; i++) {
			$('div#itemProperties ul').append('<li><img src="images/properties/'+specialProperties[i].url+'.gif" alt="'+specialProperties[i].name+'" title="'+specialProperties[i].name+'" /></li>');
		}
	}
	
	$('div#specialProperties').empty();
	if (specialProperties.length > 0) {
		$('div#specialProperties').append('<table>');
		for(i = 0; i < specialProperties.length ; i++) {
			$('div#specialProperties table').append('<tr><td><img src="images/properties/'+specialProperties[i].url+'.gif" alt="'+specialProperties[i].name+'" title="'+specialProperties[i].name+'" /></td><td>'+specialProperties[i].description+'</td></tr>');
		}
		$('div#specialProperties').append('</table>');
	}

	var image = 'images/glasses/'+json.brand_url+'/';
	$('img#product_image').attr('src', image+json.url+'.jpg');
	$('a#product_image_shadowbox').attr('href', image+'full-'+json.url+'.jpg').colorbox();

	$('.contact').attr('href', 'dotaz-k-produktu-'+productId+'.html?variant='+json.url).colorbox(
		{
			fixedWidth:"400",
			fixedHeight:"360",
			iframe:true
		}
	);

	$('#loading').hide();	
}