$(document).ready(function(){
    $('.fancybox').fancybox({
        titlePosition: 'inside'
    });
});


function CheckFieldsForm(fields) {
	fields_error = new Array();	
	$.each(fields, function(i, val) { if ($(val).val() == '' || $(val).val() == -1) fields_error.push(val); });
	$.each(fields, function(i, val) { $(val).removeClass('f-incorrect'); });	
	
	if (fields_error.length == 0) return true;
	else {	
		$.each(fields_error, function(i, val) { $(val).addClass('f-incorrect'); }); 
		return false;
	}	
}

function FormatPrice(num)
{
    var result = '';

    if (matches = /^(-?)(\d+)(\.(\d+))?$/.exec(num)) {
        sign = matches[1] ? matches[1] : '';
        whole = matches[2];
        psub = matches[4] ? matches[4] : '';

        while (/\d+\d{3}/.test(whole)) {
            whole = whole.replace(/(\d+)(\d{3})/, '$1 $2');
        }

        psub = psub.replace(/^(0+)$/, '');
        psub = (psub.length == 1) ? psub + '0' : psub;

        result = sign + whole + ((psub != '') ? '.' + psub : '');
    }

    return result;
}

// --------------- basket ------------------------ 
function AddToBasket(item_id, button) {
    $.ajax({
        url: '/ajaxer.php?x=basket_action',
        type: 'POST',
        data: {
            prod_id: item_id,
            action: 'add'
        },
        success:  function(data) {
            $(button).replaceWith('<span class="in_basket">Товар <a href="/basket/">в корзине</a></span>');
            BasketUpdateCnt();
        }
    });
}

function BasketUpdateCnt()
{
   $.ajax({
        url: '/ajaxer.php?x=basket_get_cnt',
        success:  function(data) {
            var cnt = (data == '') ? 0 : parseInt(data);
            $('#menu_basket_cnt').html('('+cnt+')');
        }
    });
}

// --------------- common function ------------------------ 
function GetCheckedValue(radioObj)
{
	if(!radioObj) return false;
	
	if(radioObj.length == undefined)
	{
		if(radioObj.checked) return radioObj.value;
		else return false;
	}
	
	for(i = 0; i < radioObj.length; i++)
	{
		if(radioObj[i].checked)	return radioObj[i].value;
	}
	
	return false;
}

// ----------------- view page -----------------------

$(window).load(function(){ init(); });
$(window).resize(function(){ CropLayers(); });

function init() {
	CropLayers();
}

function CropLayers()
{
	var width = ($(document).width() || $('body').width());
	if (width > 916)
	{
        $.each(['default', 'national', 'brass', 'guitars', 'percussion', 'sound_light'], function(key, value)
		{
			if ($('#internal-page-' + value).length)
			{
                var left = parseInt($('#internal-page-' + value).css('left'));
                var ewidth = width - (((width - 916) / 2) + left) - 0;
                $('#internal-page-' + value).css('width', ewidth + "px");
			}
		});
	}
}

// --------------- cards ---------------------
var cards = {};
$(document).ready(function(){

    $('#cards-container .card').each(function(){
        cards[$(this).attr('id')] = parseInt($(this).css('top'), 10);
    });
    
    $('#cards-container .card').mouseenter(function(){
        $(this).stop().animate({
            'top': cards[$(this).attr('id')] + 90
        }, 400);
    }).mouseleave(function(){
        $(this).stop().animate({
            'top': cards[$(this).attr('id')]
        }, 400);
    });
    
});

// ------------------ catalog --------------------
function Search()
{
	if ($('#q').val().length >= 2) {
		$('#search-form').submit();
	}
	else {
		alert('Длина поискового запроса должна быть не менее 2-х символов.');
	}
}


function SelectBrand() {
	document.location = ($('#brands').val() != 0) ? "manufacture-" + $('#brands').val() + ".html" : ".";	
} 



// ------------------- other -------------------------
function LoadMap(id, lat, lng, zoom)
{
	if (GBrowserIsCompatible())
	{
		var map = new GMap2($('#'+id)[0]);
		map.setCenter(new GLatLng(lat, lng), zoom); 
		map.setMapType(G_NORMAL_MAP);
		map.addControl(new google.maps.HierarchicalMapTypeControl());				
		
		var point = map.getCenter();
		var marker = new GMarker(point);
		map.addOverlay(marker);
		
		if (id == 'map_main')
		{
			map.addControl(new GLargeMapControl());
			GEvent.addListener(marker, 'click', function()
			{
				map.openInfoWindowHtml(point, $('#marker_'+id)[0].innerHTML);				
			});
		}
		else
		{
			map.addControl(new GSmallMapControl());			
		}
	}
}
