// JavaScript Document

$(document).ready(function(){
						   
	// Some function
	content_w ();
	product_wrapper_w ()
	
	$(window).resize(function(){
		content_w ();
		product_wrapper_w ()
	})
	
	
	// Cusel
	var params = {
		changedEl: "select.styled_select",
		visRows: 5,
		scrollArrows: true
	}
	cuSel(params);
	
	
	// Main navy
	$('ul.main_navy > li:not(.active)').hover(function(){
		$('li.active', 'ul.main_navy').addClass('on_hover');
		$(this).addClass('active');	
	}, function(){
		$(this).removeClass('active');
		$('li.active.on_hover', 'ul.main_navy').removeClass('on_hover');
	})
	
	$('ul.main_navy > li').has('div.sub_navy').hover(function(){
		$(this).children('div.sub_navy').show().end().children('span.sub_arrow').show();
	}, function(){
		$(this).children('div.sub_navy').hide().end().children('span.sub_arrow').hide();
	})
	
	
	// Gallery page
	if ( $('.show_popup').length > 0 ) {
		$('.show_popup').colorbox();
	}
	
	/*
	$('a', 'ul.photo_list').click(function(){
		var th = $(this);
		var th_href = th.attr('href');
		var arSplit = th_href.split('/')
		arSplit[arSplit.length-1] = 'b-' + arSplit[arSplit.length-1];
		var b_photo_href = arSplit.join('/');
		$('img.middle_photo', 'div.photo_big a.show_popup').attr('src', th_href).parent('a').attr('href', b_photo_href);
		return false;
	})
	*/
	if ( $('.popup-gallery a, .show_popup').length )
	   $('.popup-gallery a, .show_popup').colorbox({
            scalePhotos: true,
            maxWidth: '100%',
            maxHeight: '100%',       
       });
	
	
	// Gallery page
	if ( $('.select_matherial').length > 0 ) {
		$(".select_matherial").colorbox({width: "60%", height:"80%", iframe: true, inline: true, href:"#select_matherial"});
	}
	
		if ( $('.select_matherial_2').length > 0 ) {
		$(".select_matherial_2").colorbox({width: "60%", iframe: true, inline: true, href:"#select_matherial_2"});
	}
	
	
	// Product blocks click
	$('.prod_block, .clickable').click(function(){
		window.location = $(this).find('a').attr('href');
		return false;
	})

    initAjax();
    
    if ( $('.js-select-matherial').length )
    $('.js-select-matherial').colorbox({
        width: "60%",
		height:"80%",
        onComplete: function() {
            var $self = $(this);

            $('#colorbox a.tab').colorbox(configs);
            $('#colorbox .list_type_2 > li').click(function () {
                $self.attr('id', 'choosed-' + $(this).attr('id'));
                $self.text("изменить материал");
                //$self.parent().empty().append($self);
                $self.prev().text($(this).children('.lt_2_desc').text()).append('<br />');
                $.colorbox.close();
            });
        },
        href: function() {
            return $(this).attr('href') + '?isNaked=1';
        },
        onOpen: function() {
            configs = $.colorbox.element().data('colorbox');
        }
    });
    
    $(".carousel_wrapper").jcarousel({
        buttonPrevHTML: '<div class="prev ti">&nbsp;</div>',
        buttonNextHTML: '<div class="next ti">&nbsp;</div>',
        wrap: 'circular'
    });
    
    if ($('.top_carousel_list li').length > 5) {
		        $('.top_carousel_list').jcarousel({
		            buttonPrevHTML: '<a class="prev ti">&nbsp;</a>',
		            buttonNextHTML: '<a class="next ti">&nbsp;</a>',    
		            wrap: 'circular',
		            visible: 5                 
		        }); 
		loadImgs([
			$(".top_carousel_list li img")
			], function() {
			 $("#loader").fadeOut("slow", function () {
             $("#loader").css({visibility: "hidden"});
             $('.top_carousel_inner').css({visibility: "visible"});
             $('.top_carousel_inner').hide().fadeIn("slow");
             });
		});
    }
    
    var $imageInline;
    $('.js-image-inline')
        .live('mouseenter', function (event) {
            $self = $(this);

            $imageInline = $('<img />')
                .attr('src', $(this).attr('href'))
                .css({
                    position: 'absolute',
                    border: '3px solid #fff',
                    zIndex: 10000
                })            
                .load(function () {      
                    $(this).css({
                        top: event.pageY - this.height/2,
                        left: event.pageX + 20                    
                    });    
                    //alert(this.height);      
                    $('body').append($(this));
                });
        })
        .live('mouseleave', function () {
            $imageInline.remove();
        })
        .live('click', function () {
            $imageInline.remove();
            return false;
        });
});

function content_w (){
	var window_w = $(window).width();
	var main_w = $('div.main_wrapper');
	var footer_w = $('div.footer_wrapper');
	
	if ( window_w > 1600 ) {
		main_w.width(1600);
		footer_w.width(1600);
	} else {
		//alert(11);
		main_w.width('auto');
		footer_w.width('auto');
	}
}


function chb_content (){
	$('div.cusel.styled_select div.cusel-scroll-wrap input').each(function(){
		var this_text = $(this).parent().text();
		$(this).parents('div.cusel-scroll-wrap').prev().text(this_text);
		//console.log( $(this).parents('div.cusel-scroll-wrap').prev().text(this_text + ', ') );
	})
}

function product_wrapper_w () {
	var product_w = $('div.product_wrapper').width();
	var product_inner_w = $('div.product_inner_wrapper').width();
	
	if ( product_inner_w < 1120 ) {
		$('div.product_wrapper').width(490);
		
	} else {
		$('div.product_wrapper').width('55%');
	}
}

function initAjax(response, statusText, xhr, form) {
    $(form).removeClass('form-load');
    $('form#form-order').submit(function() {
        $(this).children('input[name=f_Material]').val($('.material').attr('id').split('_')[1]);
        $(this).children('input[name=f_Product]').val($('.product').attr('id').split('_')[1]);
    });    
    $('form.js-ajax').each(function() {
        $(this).ajaxForm({
            url: $(this).attr('action'),
            target: $(this).parent(),
            data: {isNaked: 1,isAjax: 1},
            success: initAjax,
            beforeSerialize: function($form) {
                $form.addClass('form-load');
            }
        })
    });

}

// Load IMGs BEGIN
function loadImgs(images, callback) {
	var loaded = {};
	$(images).each(function() {
		if ($(this).length > 0) {
			$(this).each(function() {
				loaded[$(this).attr("src")] = false;
			});
		}
	});
	$(images).each(function() {
		if ($(this).length > 0) {
			$(this).each(function() {
				$(new Image()).load(function() {
					loaded[$(this).attr("src")] = true;
					var l = true;
					$.each(loaded, function(i, n) {
						if (this != true) l = false;
					});
					if (l) callback.call();
				}).attr("src", $(this).attr("src"));
			});
		} else {
			callback.call();
		}
	});
}
// Load IMGs END
