$(document).ready(function() { 

	$('.galleryview').galleryView({
		panel_width: 650,
		panel_height: 435,
		show_filmstrip: false,
		frame_width: 0,
		frame_height: 0
	});
		
	$('.photos a').lightBox();
	$('a.lightbox').lightBox();

	//Init - get cart
	getcart();
	
	$('a.buy').click(function() {
		addcart($(this));
	});

	$('#ordersend').click(function() {
		$.ajax({
			   type: "POST",
			   url: json_url+'action=send',
			   data: $('#order').serialize(),
			   success: function(){
				$('#order').replaceWith('<div id="result">'+order_send_message+'</a>');
				$('html, body').stop().animate({scrollTop: 0}, 1000);
			   }
			 });
	});

	
	$('textarea.tinymce').tinymce({
			// Location of TinyMCE script
			script_url : '../_js/tiny_mce/tiny_mce.js',

			// General options
			theme : "advanced",
			plugins : "autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,images,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras",

			// Theme options
			theme_advanced_buttons1 : "bold,italic,|,undo,redo,|,cleanup,|,bullist,numlist,|,code,|,image,",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",

			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : "css/content.css",

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
	});
	
	
});


function rendershop() {
	// build sec filter
	var sec_filter='';
	if ($('#sec a.selected').length==1) sec_filter = '[sec="'+$('#sec a.selected').attr('filt')+'"]';
	$('.item').addClass('kill');
	if ($('#size a.selected').length==$('#size a').length || $('#size a.selected').length==0) {
		$('.item'+sec_filter).removeClass('kill');
	} else {
		filter = '';
		$('#size a.selected').each(function() {
			filter += ',.item[size*=",'+$(this).attr('filt')+'"]'+sec_filter;
		});
		filter = filter.substring(1);
		//alert(filter);
		$(filter).removeClass('kill');
	}
	$('.item:not(.kill)').show();
	$('.kill').hide();
}

/* cart functions */
var json_url = '/ajax.php?json=1&';
var cart_item_tpl = '<div id="c_{id}"><div class="remove" onclick="removecart(\'{id}\')"></div><i><img src="{img}" />{title}</i><b>{price}</b> руб.<span><input value="{count}" class="count" onchange="changecart(\'{id}\',this.value)" /> шт.</span></div>';
var summ_tpl = '<div id="summ">Сумма: {summ} руб. <a id="orderbutton" class="button" href="/order/">Оформить заказ</a></div>';
var order_send_message = 'Ваш заказ принят. В ближайшее время с вами свяжется менеджер для уточнения времени доставки.';

function getcart() {
	$('#cart').append('<img src="/i/loading2.gif" id="loading" />');
	$.getJSON(json_url+'action=getcart', function(data) {
		$.each(data, function(id,item) {
			//alert($.nano(cart_item_tpl,item));
			if (id!='summ') $('#cart').append($.nano(cart_item_tpl,item));
		});
		$('#summ').remove();
		if (data['summ']>0) $('#cart').append($.nano(summ_tpl,data));
		$('#overallsumm').val(data['summ']);
		$('#loading').remove();
	});
}

function addcart(obj) {
	$('#cart').append('<img src="/i/loading2.gif" id="loading" />');
	$.getJSON(json_url+'action=add&id='+obj.attr('ref')+'&title="'+encodeURIComponent(obj.attr('title'))+'"&price='+obj.attr('price')+'&img='+obj.attr('img'), function(data) {
		if ($('#c_'+data['id']).length) $('#c_'+data['id']).replaceWith($.nano(cart_item_tpl,data)); else $('#cart').append($.nano(cart_item_tpl,data));
		$('#summ').remove();
		$('#loading').remove();
		if (data['summ']>0) $('#cart').append($.nano(summ_tpl,data));
	});
}

function changecart(id,count) {
	$.getJSON(json_url+'action=change&id='+id+"&count="+count, function(data) {
		if (data['count']>0) $('c_'+data['id']).replaceWith($.nano(cart_item_tpl,data)); else $('#c_'+data['id']).remove();
		$('#summ').remove();
		if (data['summ']>0) $('#cart').append($.nano(summ_tpl,data));
		$('#overallsumm').val(data['summ']);
	});
}

function removecart(id) {
	$.getJSON(json_url+'action=remove&id='+id, function(data) {
		$('#c_'+data['id']).remove();
		$('#summ').remove();
		if (data['summ']>0) $('#cart').append($.nano(summ_tpl,data));
		$('#overallsumm').val(data['summ']);
	});
}

function showdesign() {
	$('#showdesign-link').addClass('selected');
	$('#showsecret-link').removeClass('selected');
	$('#secretdesign').show();
	$('#secretform').hide();
}

function showsecret() {
	$('#showdesign-link').removeClass('selected');
	$('#showsecret-link').addClass('selected');
	$('#secretdesign').hide();
	$('#secretform').show();
}


// facebook
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));

