 /*function initialize() {
    var myOptions = {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
}*/

$(document).ready(function() {
    //if(window.location.href.indexOf('\/online-sales\/cart\/') > -1 || window.location.href.indexOf('\/online-sales\/checkout\/') > -1) {
    if($('body.home').length == 0) {
        $('.b-head-menu').removeClass('b-head-menu_hide');
        loadContent();
    }
    
    loadAjax();
	
	if($('body').attr('class').split(' ')[0] == 'splash') {
		// adjust header height
		var height = $(window).height() - 612;
		if(height < -122) {
            height = 0;
        }
		else if(height < 0) {
			height = 122 + height;
		}
		else {
			height = 122;
		}
		$('.b-head_empty').animate({
			height: height + 'px'
		});
	}
});

function loadAjax() {
	$('.b-head-menu__link[href*="products"], .b-body-menu-table a[href*="products"]').each(function(i, item) {
		var href = $(item).attr('href');
		if(href.indexOf('winter') < 0) {
			$(item).attr('href', href.replace('products', 'products/winter'));
		}
	});
	$('.b-head-menu__link[href*="culture"], .b-body-menu-table a[href*="culture"]').each(function(i, item) {
		var href = $(item).attr('href');
		if(href.indexOf('activities') < 0) {
			$(item).attr('href', href.replace('culture', 'culture/activities'));
		}
	});
	$('.b-head-menu__link, .b-body-menu-table a, .b-body-menu-other__link, .breadcrumbs a, .sidebar a, .order-links a.my-orders, .content-submenu a').ajaxlinks({
		load_from: '.b-body',
		load_to: '.b-body',
		loader: '<div class="b-body-loader"><img src="/media/image/loading_icon.gif" alt="" class="b-body-loader__img" /></div>',
		prefix: '/home/',
		use_sammy: true,
		smooth_height: false,
		callback: ajaxCallback
	});
}

function ajaxCallback(content, link, params) {
	if(params.hasOwnProperty('store') || params['path'] == '/stores/locator/') {
		loadMap(params.store);
	}
	else {
		loadContent();
	}
}

function loadContent() {
    $('.b-head-menu').removeClass('b-head-menu_hide');
	
	if ($('#sliderkit').length > 0) {
		// adjust slides size
		var height = $(window).height()-280;
		var width = parseInt(height/0.66625);
		if (width > 1000) {
			width = 1000;
			height = 666;
		}
		var navitems = Math.floor(width / 85);
		$('#slider-wrapper').css('margin-left', parseInt((1000-width)/2)+'px');
		$('#sliderkit').css({
			'width': width+'px',
			'height': height+'px'
		});
		$('#sliderkit').find('.sliderkit-panel, .sliderkit-panel-image').css({
			'width': width+'px',
			'height': height+'px'
		});
		$('#sliderkit .sliderkit-panel img').css({
			'max-width': width+'px',
			'height': height+'px'
		});
		$('#sliderkit').find('.sliderkit-nav, .sliderkit-panel-textbox, .sliderkit-panel-overlay').css({
			'width': width+'px'
		});		
		$('#sliderkit').find('.sliderkit-panel-text').css({
			'width': (width-100)+'px'
		});	
		$('#sliderkit .sliderkit-go-btn').css({
			'height': height+'px'
		});
		
		// init slider
        var auto = false;
		if($('.page-slug').data('slug') == 'brand' || $('.page-slug').data('slug') == 'music') {
			var auto = true;
		}
		$('#sliderkit').sliderkit({
            mousewheel: true,
            keyboard: true,
            shownavitems: navitems,
            //navfx:"none",
            panelbtnshover: true,
            auto: auto,
            circular: true,
            navscrollatend: true
        });
    }
	else if($("#sliderkit-tabs").length > 0) {
		// Tabs > No height
        $("#sliderkit-tabs").sliderkit({
			mousewheel: true,
            auto:false,
            tabs:true,
            freeheight:true,
            circular:true
        });
	}
	
    var initScrollPane = function(resize) {
		if ($('.b-scroll-pane').length > 0) {
			$('.b-scroll-pane').jScrollPane({
				autoReinitialise: true
			});
			
			if(resize) {
				// - scrollpane height
				function resizeScrollPane(){
					if ($('.page-slug').data('slug') != 'stores') {
						$('.b-scroll-pane').height($(window).height() - 122 - 80);
					}
				};
				
				$(window).resize(resizeScrollPane);
				
				resizeScrollPane();
			}
		}
    };
    
    var page = $('.page-slug').data('slug');	
    switch (page) {
        case 'brand': 
            break;
        case 'winter':
        	loadAjax(); 
            break;
        case 'spring': 
        	loadAjax();
            break;
        case 'autumn': 
        	loadAjax();
            break;
        case 'summer': 
        	loadAjax();
            break;
        case 'stores':                          
            //initialize();
            initScrollPane(false);
            initStores();            
            break;
		case 'products':
		case 'culture':
		    loadAjax();
		    initScrollPane(true);
			break;
		default:
            loadAjax();
			initScrollPane(true);
            break;
    }
	
	$('.b-head-menu__link').each(function() {
		if (window.location.href.indexOf($(this).data('url')) > -1) {
            $(this).addClass('active-link');
        } else {
            $(this).removeClass('active-link');
        }
    });
};

function initStores() {
	// adjust header height
	var height = $(window).height() - 612;	
	if(height < 140) {
		height = 140;
	}
	if(height > 290) {
		height = 290;
	}
	$('#stores-img').css('height', height+'px');
		
	// static map handler
    $('.store').live('click', function() {
        $('.store').removeClass('active-link');
        $(this).addClass('active-link');
        var mapurl = [
            'http://maps.googleapis.com/maps/api/staticmap?center=',
            $(this).data('lat'),
            ',',
            $(this).data('lng'),
            '&zoom=14&size=360x360&maptype=roadmap&markers=color:red|color:red|label:A|',
            $(this).data('lat'),
            ',',
            $(this).data('lng'),
            '&sensor=false'
        ].join('');
        $('#static-map').attr('src', mapurl);
		$('#map-link').attr('href', [
            '#!path=/stores/locator/|store=',
			$(this).data('slug')
		].join(''));
    });	
    $('.store:first').trigger('click');
}

function findImg(link) {console.log(link);
	var href = $(link).attr('href');
    var img = $('#menu-images').find(['a[href="', href, '"] img'].join(''));
	console.log(img);
	return img;
}

$(function() {
    // - show picture under cursor
    $('.b-body-menu-table-top__img').hover(
    function() {
        $(this).animate({'opacity': '1'}, 'slow')
    }, 
    function() {
        $(this).animate({'opacity': '0.45'}, 'slow');
    });
    $('.b-body-menu-table-bottom__link').hover(
    function() {
        var img = findImg(this);
		$(img).animate({'opacity': '1'}, 'slow');
    }, 
    function() {
        var img = findImg(this);
        $(img).animate({'opacity': '0.45'}, 'slow');
    });		
		
    // - move pick
    $('.b-body-menu').mousemove(function(e) {
        $('.b-body-menu-line__pick').css('width', e.pageX - this.offsetLeft);
    });
    
    // - transition effect
    $('.b-body-menu-table-bottom__link, .b-body-menu-table-top__link, .b-body-menu-other__link').click(function(e) {
        var url = $(this).attr('href');
		var dataUrl = $(this).attr('data-url');
        
        $('.b-body-menu').animate({"left": "+=200px", 'top': '-=200px', 'width': '-=200px'}, 
        "slow", 
        function() {
            $(this).hide();
            $('.b-head-menu').removeClass('b-head-menu_hide');
            
			$.sammy('.b-head-menu__link').setLocation(url);
        });
		
        return false;
    });
});
