
var popup = {
	open: function(src) {
		popup.close();

		$('#main').append('<div class="popup popupVideo">' +

		'<div class="popupA"><div class="popupB"><div class="popupC"><div class="popupD"><div class="popupE"><div class="popupF">' +
		'<div class="popupInner">' +
		'<a href="#x" class="close" onclick="popup.close();return false;"><span class="forBlind">X</span></a>' +

		'<!--[if !IE]> -->' +
		'<object class="flashPlayerObject" type="application/x-shockwave-flash" data="' + src + '" width="530" height="327" id="flashPlayerObject">' +
		'<!-- <![endif]-->' +
		'<!--[if IE]>' +
		'<object id="flashPlayerObject" class="flashPlayerObject" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="530" height="327" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0">' +
		'<param name="movie" value="' + src + '" />' +
		'<!--><!-- -->' +
		'<param name="loop" value="true" />' +
		'<param name="menu" value="false" />' +
		'<param name="wmode" value="transparent" />' +
		'<param name="allowscriptaccess" value="always" />' +
		'<param name="allowfullscreen" value="true" />' +
		'<div class="flashAlternative">' +

		'</div>' +
		'</object>' +
		'<!-- <![endif]-->' +

		'</div>' +
		'</div></div></div></div></div></div>' +
		'</div>');
	},
	close: function(){
		if (popup.parentOfOpened) {
			$(popup.parentOfOpened).append($('#main').find('.popupContent'));
			$(popup.parentOfOpened).removeClass('selected');
			popup.parentOfOpened = false;
		}
		$('#main').children('.popup').remove();
	},
	parentOfOpened: false
}

var places = {
	config: {},
	init: function(){
		places.el.wrapper = $('#places');
		// no places
		if (!places.el.wrapper.length)
			return false;
		// no google maps
		if (typeof(google.maps) !== 'object')
			return false;

		places.el.wrapper.hide();
		// create map
		places.createMap();
		// find items and create locations
		places.el.items = places.el.wrapper.children('.item');
		places.el.items.each(function(){
			places.createLocation($(this));
		});
	},
	createMap: function() {
		var pmc = $('#placesMapCenter').text();
		var pos = "";
		var zoom = 7;

		if (pmc != undefined && pmc.indexOf("|") >= 0) {
			var arr = pmc.split("|");
			pos = arr[0];
			zoom = (parseInt(arr[1]) <= 0) ? zoom : parseInt(arr[1]);
		} else {
			pos = pmc;
		}
	 
		// get gps coords
		var latlng = places.parseGps(pos);
		// define center of map
		var mapCenter = new google.maps.LatLng(latlng[0], latlng[1]);
		// define map options
		var mapOptions = {
			zoom: zoom,
			center: mapCenter,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
					disableDefaultUI: false,
					mapTypeControl: false,
			mapTypeControlOptions: {
				style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
				position: google.maps.ControlPosition.TOP_RIGHT
			},
			navigationControl: true,
			navigationControlOptions: {
				style: google.maps.NavigationControlStyle.ZOOM_PAN,
				position: google.maps.ControlPosition.TOP_RIGHT
			},
			scaleControl: true,
			scaleControlOptions: {
				position: google.maps.ControlPosition.TOP_LEFT
			}
		};
		// create map from options and center
		places.map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
	},
	createLocation: function(el){
		var locoptions = {};

		var ico = "gmap-marker.png";
		var loc = el.find('.location').text();
		if (loc == undefined || loc == "") {
			ico = "gmap-marker1.png";
			loc = el.find('.location1').text();
	 	}
	
		// get gps coords
		var latlng = places.parseGps(loc);
		// create gps location
		locoptions.markerPos = new google.maps.LatLng(latlng[0], latlng[1]);
		
		// create marker
		locoptions.marker = new google.maps.Marker({
			position: locoptions.markerPos,
			title: el.find('.title').text(),
			icon: '/public/images/' + ico,
			map: places.map
		});

		// create content
		var htmlContent = '';
		htmlContent += '<div class="popup popupMap"><div class="popupA"><div class="popupB"><div class="popupC"><div class="popupD"><div class="popupE"><div class="popupF"><div class="popupInner"><div class="cleaned">';
		htmlContent += el.html();
		htmlContent += '</div><span class="arrow"><!--	--></span></div><!-- #popupInner --></div></div></div></div></div></div></div><!-- .popup -->';

		// create info window
		locoptions.infoWindow = new InfoBox({
			// infoWindow compatible
			content: htmlContent,
			//pixelOffset: 10,
			//position: markerPos,
			maxWidth: 320,
			//infoBox only
			disableAutoPan: false,
			pixelOffset: new google.maps.Size(-64, -188),
			zIndex: null,
			boxStyle: {
				width: "320px"
			},
			closeBoxMargin: "0",
			closeBoxURL: "/public/images/icon-close.png",
			infoBoxClearance: new google.maps.Size(20, 20),
			isHidden: false,
			pane: "floatPane",
			enableEventPropagation: true
		});
		
		// on infoBox open
		google.maps.event.addListener(locoptions.infoWindow, 'domready', function(){
			// set class to close button
			$('.popupMap').prev('img').attr('style','').addClass('popupClose');
		});
		
		// bind open info window on marker click event
		google.maps.event.addListener(locoptions.marker, 'click', function(){
			// Go directly to detail when mobile
			if(window.mobileBrowser){
				window.location.href = el.find('.links a:first').attr('href')+'#text';
			}
			else {
				// close all boxes
				for(var i = 0; i < places.locations.length; i++)
				{
					places.locations[i].infoWindow.close();
				}
				locoptions.infoWindow.open(places.map, locoptions.marker);
			}
		});
		
		// store link to array
		places.locations[places.locations.length] = locoptions;
	},
	parseGps: function(text){
		return text.replace(/\s/, '').split(/,|;/);
	},
	el: {},
	locations: []
};

/**
 * Tweaks for mobile browsers
 */
function mobileFeatures(){

    // Remove blank content and sidebar
    if(!$('#content').html() || $('#content').html().length < 50){
        $('#content').remove();
    }
    if(!$('#sidebar').html() || $('#sidebar').html().length < 50){
        $('#sidebar').remove();
    }

    // Move some elements in code
    $('#mainMenu').addClass('relative').insertAfter( $('#header').addClass('relative') );
    $('#contentInfo').addClass('relative').insertAfter( $('#productPhoto').addClass('relative') );
    $('#sidebar').prependTo( $('#mainInner') );

    // Add anchors to submenu
    /*
    $('#sidebar .subMenu a').each(function(){
        if($(this).attr('href').match('#')==null){
            $(this).attr('href', $(this).attr('href')+'#productPhoto');
        }
    });
    */

    // Remove dummy item from carousel
    $('#carousel .item1').remove();

    mobileVideos();
}

function mobileVideos(){
    // Convert media player links to files, that player plays
    $('#carousel .video a').each(function(){
        // Extract filename from link
        var file = $(this).attr('href').match(/file=([^\&]*)/);
        if(file!=null){
            // Convert flv link to mpeg link
            var mobile = file[1].replace(/\.flv$/, '.3gp');
            if(mobile!=null){
                $(this).attr('href', mobile);
            }
        }
    });
}

//--------------------------------------------------------------------------

$(document).ready(function() {
	//window.mobileBrowser = $.browser.mobile;
	if(navigator.userAgent.match(/iPad/i) != null) {
		mobileVideos();
	}
	
	if (window.mobileBrowser) {
		mobileFeatures();
	}
	if (!window.mobileBrowser) {
		Cufon.replace('h1,h2,#pageSubtitle,#carouselTitle,.cufon');
		Cufon.replace('#mainMenu a,#clientZone .a,#carousel .title', { hover: true });
	}

	hs.registerOverlay({
		html: '<div class="galleryClosebutton" onclick="return hs.close(this)" title="Zavřít"></div>',
		position: 'top right',
		fade: 2
	});
	hs.align = 'center';
	hs.targetX = null;
	hs.targetY = 'main 50px';
	hs.wrapperClassName = 'borderless';
	hs.outlineType = 'drop-shadow';
	hs.dimmingOpacity = 0.75;
	hs.dimmingDuration = 200;
	hs.easing = 'linearTween';
	hs.expandDuration = 200;
	hs.restoreDuration = 150;

	if(!window.mobileBrowser)
	{
		wwwMenuOffsetTop = parseInt($('#wwwMenu').css('top'));

		//$(window).scroll(function(){
		var interval = setInterval(function(){
			$('#wwwMenu').animate({ top:$(window).scrollTop()+wwwMenuOffsetTop+"px" }, { queue: false, duration: 200 });
		//});
		}, 100);
	} // if(!window.mobileBrowser)


	/**
	* CAROUSEL
	*/
	if( $('#carousel .item').length + $('#carousel .item1').length > ( window.mobileBrowser ? 1 : 5) ) {
		var selectedIndex = $('#carousel .selected').index();
		var startIndex = (selectedIndex) ? selectedIndex : 0;
		startIndex = (startIndex < 0) ? 0 : startIndex;

		$('#carousel .outer').height($('#carousel .items').height()).after('<a href="#" class="prev browse left" onclick="return false"><!--	--></a><a href="#" class="next browse right" onclick="return false"><!--	--></a>');
		$('#carousel .outer').serialScroll({
			items:'li',
			prev:'#carousel a.prev',
			next:'#carousel a.next',
			offset: 0,
			start: startIndex,
			duration: window.mobileBrowser ? 100 : 1100,
			force:true,
			stop:true,
			lock:false,
			cycle:true,
			easing:'easeOutQuart',
			jump: false,
			exclude: window.mobileBrowser ? 0 : 5
		});

		/*if(selectedIndex) {
			carousel.click( selectedIndex );
		}*/
	} else if($('#carousel').length) {
		$('#carousel .outer').height($('#carousel .items').height()).after('<a href="#" class="prev browse left disabled" onclick="return false"><!--	--></a><a href="#" class="next browse right disabled" onclick="return false"><!--	--></a>');
	}

	if(!window.mobileBrowser)
	{
		if(typeof($.jScrollPane)=='object'&&$('#scrollContent').length) {
			$('#main').addClass('scrollActive');
			$('#scrollContent').jScrollPane( {showArrows: false, scrollbarWidth: 23, scrollbarMargin: 0, height: 493 });
		}

		$('.infiledlabel label').addClass('infield').inFieldLabels();

	} // if(!window.mobileBrowser)

	//places.init();

	GGal = function (obj) {
		// Normal link when mobile browser
		if(window.mobileBrowser){
			return true;
		}
		popup.close();
		var parent1 = $('#carousel .selected');
		parent1.removeClass('selected');
		
		var parent = $(obj).parents('.item');
		parent.addClass('selected');
		
		hs.expand(obj);
		 
		return false;
	}

	GPic = function (obj, src) {
		// Normal link when mobile browser
		if(window.mobileBrowser){
			return true;
		}
		popup.close();
		var parent1 = $('#carousel .selected');
		parent1.removeClass('selected');
		
		var parent = $(obj).parents('.item');
		parent.addClass('selected');
		
		var pic = $('#productPhoto img');
		pic.attr("src", src); 
		
		return false;
	};
	
	GVid = function (obj, src) {
		// Normal link when mobile browser
		if(window.mobileBrowser){
			return true;
		}
		popup.close();
		var parent1 = $('#carousel .selected');
		parent1.removeClass('selected');
		
			var parent = $(obj).parents('.item');
			popup.open(src);
				popup.parentOfOpened = parent;
				parent.addClass('selected');
		
		return false;
	};
	
	$("a.autoStart").trigger('click');

	if($('#bottBox').length) {
		btnOffsetBottom = parseInt($('#bottBox').css('bottom'));
		
		$('#bottBox').animate({ top: (f_clientHeight() + $(window).scrollTop() - btnOffsetBottom - 78) + "px" }, { queue: false, duration: 200 });
		$(window).scroll(function() {
			$('#bottBox').stop().animate({ top: (f_clientHeight() + $(window).scrollTop() - btnOffsetBottom - 78) + "px" }, { queue: false, duration: 200 });
		});
	};
	
	
	$(window).resize(function() {
		if ($(window).width() > 1160) {
			$('#wwwMenu').stop().show(800);
		} else {
			$('#wwwMenu').stop().hide(800);
		}
	}).trigger('resize');
});

jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

jQuery.easing.easeOutExpo = function (x, t, b, c, d) {
	return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

;(function(c){var a=c.scrollTo=function(f,e,d){c(window).scrollTo(f,e,d)};a.defaults={axis:"xy",duration:parseFloat(c.fn.jquery)>=1.3?0:1};a.window=function(d){return c(window)._scrollable()};c.fn._scrollable=function(){return this.map(function(){var e=this,d=!e.nodeName||c.inArray(e.nodeName.toLowerCase(),["iframe","#document","html","body"])!=-1;if(!d){return e}var f=(e.contentWindow||e).document||e.ownerDocument||e;return c.browser.safari||f.compatMode=="BackCompat"?f.body:f.documentElement})};c.fn.scrollTo=function(f,e,d){if(typeof e=="object"){d=e;e=0}if(typeof d=="function"){d={onAfter:d}}if(f=="max"){f=9000000000}d=c.extend({},a.defaults,d);e=e||d.speed||d.duration;d.queue=d.queue&&d.axis.length>1;if(d.queue){e/=2}d.offset=b(d.offset);d.over=b(d.over);return this._scrollable().each(function(){var l=this,j=c(l),k=f,i,g={},m=j.is("html,body");switch(typeof k){case"number":case"string":if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(k)){k=b(k);break}k=c(k,this);case"object":if(k.is||k.style){i=(k=c(k)).offset()}}c.each(d.axis.split(""),function(q,r){var s=r=="x"?"Left":"Top",u=s.toLowerCase(),p="scroll"+s,o=l[p],n=a.max(l,r);if(i){g[p]=i[u]+(m?0:o-j.offset()[u]);if(d.margin){g[p]-=parseInt(k.css("margin"+s))||0;g[p]-=parseInt(k.css("border"+s+"Width"))||0}g[p]+=d.offset[u]||0;if(d.over[u]){g[p]+=k[r=="x"?"width":"height"]()*d.over[u]}}else{var t=k[u];g[p]=t.slice&&t.slice(-1)=="%"?parseFloat(t)/100*n:t}if(/^\d+$/.test(g[p])){g[p]=g[p]<=0?0:Math.min(g[p],n)}if(!q&&d.queue){if(o!=g[p]){h(d.onAfterFirst)}delete g[p]}});h(d.onAfter);function h(n){j.animate(g,e,d.easing,n&&function(){n.call(this,f,d)})}}).end()};a.max=function(j,i){var h=i=="x"?"Width":"Height",e="scroll"+h;if(!c(j).is("html,body")){return j[e]-c(j)[h.toLowerCase()]()}var g="client"+h,f=j.ownerDocument.documentElement,d=j.ownerDocument.body;return Math.max(f[e],d[e])-Math.min(f[g],d[g])};function b(d){return typeof d=="object"?d:{top:d,left:d}}})(jQuery);
;(function(a){var b=a.serialScroll=function(c){return a(window).serialScroll(c)};b.defaults={duration:1e3,axis:"x",event:"click",start:0,step:1,lock:!0,cycle:!0,constant:!0};a.fn.serialScroll=function(c){return this.each(function(){var t=a.extend({},b.defaults,c),s=t.event,i=t.step,r=t.lazy,e=t.target?this:document,u=a(t.target||this,e),p=u[0],m=t.items,h=t.start,g=t.interval,k=t.navigation,l;if(!r){m=d()}if(t.force){f({},h)}a(t.prev||[],e).bind(s,-i,q);a(t.next||[],e).bind(s,i,q);if(!p.ssbound){u.bind("prev.serialScroll",-i,q).bind("next.serialScroll",i,q).bind("goto.serialScroll",f)}if(g){u.bind("start.serialScroll",function(v){if(!g){o();g=!0;n()}}).bind("stop.serialScroll",function(){o();g=!1})}u.bind("notify.serialScroll",function(x,w){var v=j(w);if(v>-1){h=v}});p.ssbound=!0;if(t.jump){(r?u:d()).bind(s,function(v){f(v,j(v.target))})}if(k){k=a(k,e).bind(s,function(v){v.data=Math.round(d().length/k.length)*k.index(this);f(v,this)})}function q(v){v.data+=h;f(v,this)}function f(B,z){if(!isNaN(z)){B.data=z;z=p}var C=B.data,v,D=B.type,A=t.exclude?d().slice(0,-t.exclude):d(),y=A.length,w=A[C],x=t.duration;if(D){B.preventDefault()}if(g){o();l=setTimeout(n,t.interval)}if(!w){v=C<0?0:y-1;if(h!=v){C=v}else{if(!t.cycle){return}else{C=y-v-1}}w=A[C]}if(!w||t.lock&&u.is(":animated")||D&&t.onBefore&&t.onBefore(B,w,u,d(),C)===!1){return}if(t.stop){u.queue("fx",[]).stop()}if(t.constant){x=Math.abs(x/i*(h-C))}u.scrollTo(w,x,t).trigger("notify.serialScroll",[C])}function n(){u.trigger("next.serialScroll")}function o(){clearTimeout(l)}function d(){return a(m,p)}function j(w){if(!isNaN(w)){return w}var x=d(),v;while((v=x.index(w))==-1&&w!=p){w=w.parentNode}return v}})}})(jQuery);

window.onload = function(){
	//	init_map();
	places.init();
};

