var W3CDOM = (document.createElement && document.getElementsByTagName);
function init_file() {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	fakeFileUpload.appendChild(document.createElement('input'));
	var image = document.createElement('img');
	image.src='/images/add_button.gif';
	fakeFileUpload.appendChild(image);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].getAttribute('noscript')) continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		if (x[i].value)
			x[i].onchange();
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

$(document).ready(function(){
	$('#my-dropdown, #my-dropdown2, #my-dropdown3, #my-dropdown-lift').sSelect();
	$("#data_form").validate();
	$("#cut").css('display','none');
	if ($("#cut").length > 0)
	{
		$('#about').append('<br><a id="more" href="#" onclick="more();return false;">Подробнее</a>');
	}
	if($('#main_img').length){
		FirstGalleryImg();
	}
	
	if($("#other-cities").length){
		$("#other-cities").fancybox({
			'opacity'			: true,
			'overlayShow'		: true,
			'transitionIn'		: 'fade',
			'transitionOut'		: 'fade',
			'centerOnScroll'	: true,
			'hideOnContentClick': false,
			'showCloseButton'	: false,
			'titleShow'			: false,
			'overlayOpacity'	: 0.6,
			'overlayColor'		: '#fff',
			'padding'			:0,
			'onComplete'		: function() {
				setTimeout("$.fancybox.close();", 108000);
				if ($('.choose-city .newListSelected').length){
					return;
				}
				else {
					$('#cities-list').sSelect();
				}
		    }
		});
	}	
	
});	

$(document).ready(function(){
	function smartColumns() {
		$("ul.column").css({ 'width' : "100%"});
		var colWrap = $("ul.column").width();
		var colNum = Math.floor(colWrap / 200);
		var colFixed = Math.floor(colWrap / colNum);
		$("ul.column").css({ 'width' : colWrap});
		$("ul.column li").css({ 'width' : colFixed});
		var colSingle = Math.floor(colFixed - 18);
		$(".single-elements-li").css({ 'width' : colSingle});	
		$(".bottom-menu-li").css({ 'width' : colSingle});		
	}	
	smartColumns();	
	$(window).resize(function () {
		smartColumns();		
	}); 		
});

jQuery(document).ready(function() {
	init_gallery();
});

function init_gallery() {
	if ($("#mycarousel").length) {
		jQuery('#mycarousel').jcarousel({
			scroll: 4
		});
	}
	jQuery('#gallery-container img').click(function () {
		if($(this).hasClass('child')){
			jQuery('#gallery-wrapper').css('width','821px');
			jQuery('#gallery-vertical-container').addClass('active');
			jQuery('#mycarousel-ver').css('top','0px');
			jQuery('#mycarousel-ver').jcarousel({
				vertical: true,
				scroll: 2,
				start: 1
			});
		}
		else{
			jQuery('#mycarousel-ver').css('top','0px');
			jQuery('#gallery-wrapper').css('width','662px');
			jQuery('#gallery-vertical-container').removeClass('active');
		}
	});
}

function FirstGalleryImg() {
	$('#main_img').css('width','');
	$('#main_img').css('height','');
	var ImgWidth = $('#main_img').width();
	var ImgHeight = $('#main_img').height();
	var k =  ImgHeight/ImgWidth;
	if (ImgWidth < 662){
		if (ImgHeight < 500){
			$('#main_img').css({'width': ImgWidth, 'height': ImgHeight });
		}
			else {
			$('#main_img').css('height','500px'); 
		}
	}
	else {
			if (ImgHeight < 500){
			$('#main_img').css('width', '662px');
		}
		else {
			if (k > 1){
					$('#main_img').css('height', '500px');
				var variable = parseInt(500/k);
				$('#main_img').css('width', variable);
			}
			else {
					$('#main_img').css('width', '662px');
			}
		}
	}
}


function GalleryImg() {
	if($('#main_img').length) {
		$('#main_img').css('width','');
		$('#main_img').css('height','');
		var ImgWidth = $('#main_img').width();
		var ImgHeight = $('#main_img').height();
		var k =  ImgHeight/ImgWidth;
		if (ImgWidth < 662){
			if (ImgHeight < 500){
				$('#main_img').css({'width': ImgWidth, 'height': ImgHeight });
			}
			else {
				$('#main_img').css('height','500px'); 
			}
		}
		else {
			if (ImgHeight < 500){
				$('#main_img').css('width', '662px');
			}
			else {
				if (k > 1){
					$('#main_img').css('height', '500px');
					var variable = parseInt(500/k);
					$('#main_img').css('width', variable);
				}
				else {
					$('#main_img').css('width', '662px');
				}
			}
		}
	}
}


function PutImg(ImgName) {
	$("#main_img").attr("src", ImgName);
	$("#main_link").attr("href",ImgName);
	$('#main_link img').css('width','');
	$('#main_link img').css('height','');
	GalleryImg();	
}

function SwitchToElem()
{
	if($('#sel1 :selected').text() == 'композиция')
	{
		$('#sel3').css('visibility','visible');
		
		$.post('/catalog/linebycomp/', function(data) {
			  $('#sel2').html(data);
			  $('#my-dropdown2').sSelect();
		});
	}
	else 
	{
		$('#sel3').css('visibility','hidden');
		
		$.post('/catalog/linebycomp/' + $('#sel1 :selected').val(), function(data) {
			  $('#sel2').html(data);
			  $('#my-dropdown2').sSelect();
		});
	}
}

function SwitchToComp()
{
	if($('#sel3 :selected').text() == 'элементы')
	{
		$('#sel1').css('visibility','visible');
		
		$.post('/catalog/linebyelem/', function(data) {
			  $('#sel2').html(data);
			  $('#my-dropdown2').sSelect();
		});
	}
	else 
	{
		$('#sel1').css('visibility','hidden');
		
		$.post('/catalog/linebyelem/' + $('#sel3 :selected').val(), function(data) { 
			  $('#sel2').html(data);
			  $('#my-dropdown2').sSelect();
		});
	}
}

function ResetLines()
{
	$.post('/catalog/linebycomp/', function(data) {
		  $('#sel2').html(data);
		  $('#my-dropdown2').sSelect();
	});
}

function checkSearch()
{
	if($('#sel1 :selected').text() == 'композиция' && 
		$('#sel2 :selected').text() == 'линия' && 
		$('#sel3 :selected').text() == 'элементы' &&
		!$('#price_to').val() &&
		!$('#price_from').val() &&
		( $('#name').val() == 'артикул' || !$('#name').val() )
		)
	{
		return false; 
	}
	else
	{
		return true; 
	}
}

function resetSearch()
{ 
	$('#sel1').css('visibility','visible');
	$('#sel2').css('visibility','visible');
	$('#sel3').css('visibility','visible');
	
	$('#my-dropdown').getSetSSValue('0');
	$('#my-dropdown2').getSetSSValue('0');
	$('#my-dropdown3').getSetSSValue('0');
}

function popup()
{
	tb_show("Корзина","/catalog/showcart/?height=100&width=250", null);
}

 function Increment(id) {
        value = parseInt($('#qty_'+id).val())+1;
        $('#qty_'+id).val(value);
        xajax_updateCart(id,value);
 }

 function Decrement(id) {
        value = parseInt($('#qty_'+id).val())-1;
        if (value>0) {
            $('#qty_'+id).val(value);
            xajax_updateCart(id,value);
        }
 }
 
 function IncrementLevel() {
     value = parseInt($('#level').val())+1;
     $('#level').val(value);
}

function DecrementLevel() {
     value = parseInt($('#level').val())-1;
     if (value>0) {
         $('#level').val(value);
     }
}

function ShowForm(){
	if($('#more-form').css('display') =='none'){
		$('#more-form').css('display','block');	
		$('#small-form-button').css('display','none');
	}
	else {
		$('#more-form').css('display','none');	
		$('#small-form-button').css('display','block');
	}
}


function check_sendform()
{
		if ($("#confirm").is(":checked") )
		{
			$("#submit").removeClass("disable");
			$("#submit").attr("disabled",false);
			return true;
		}
		else
		{
			$("#submit").addClass("disable");
			$("#submit").attr("disabled",true);
			return false;
		}	
}

function more()
{
	$('#cut').toggle();
	if ( $('#cut').is(':visible'))
	{
		$('#more').text('Скрыть');
	} 
	else
	{
		$('#more').text('Подробнее');
	}
}

