/**
 * @author: Dariusz Pobożniak | http://pobozniak.pl
 */

$(function() {
	showMenu();
	offerForm();
	$('.lightbox').fancybox();
	sendEmail();
	createTabs();
	googleMap();
})

function showMenu() {
	$('#header > ul > li').hover(function() {
		if ($(this).attr('id') == 'tab-offer') {
			$(this).css('background', 'url(gfx/bgTopNavOLeft.gif) 0 0 no-repeat');
			$(this).find('a:eq(0)').css('background', 'url(gfx/bgTopNavORight.gif) 100% 0 no-repeat');
		} else {
			$(this).css('background', 'url(gfx/bgTopNavLeft.gif) 0 0 no-repeat');
			$(this).find('a:eq(0)').css('background', 'url(gfx/bgTopNavRight.gif) 100% 0 no-repeat');
		}
		$(this).find('ul').show();
	}, function() {
		$(this).css('background', '');
		$(this).find('a:eq(0)').css('background', '');
		$(this).find('ul').hide();
	})
	$('#tab-offer > a').click(function() { return false; })
}

function offerForm() {
	if ($('#offerlist').length > 0) {
		$('#offerlist form').hide();
		$('#offerlist .price .more').click(function() {
			$('#offerlist form').hide();
			$('#offerlist .price .more').css('display','');
			$(this).css('display','none');
			$(this).parent().parent().next('form').slideDown('slow');
			return false;
		});
		$('#offerlist .cancel').click(function() {
			$(this).parent().parent().slideUp('slow', function() {
				$(this).parents('li').find('.price .more').css('display','');
			});
			
			return false;
		})
	}
}

function sendEmail() {
	$('input[type="submit"]').click(function() {
		var form = $(this).parent().parent();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		
		form.find('input[name!="company"]').each(function() {
			if ($(this).val() == '') {
				$(this).addClass('error');
				hasError = true;
			} else {
				if ($(this).attr('name') == 'email') {
					if (!emailReg.test($(this).val())) {
						$(this).addClass('error');
					} else {
						$(this).removeClass('error');
					}
				} else {
					$(this).removeClass('error');
				}	
			}
		})
		
		if (hasError == true) {
			return false;
		}
	})
}

/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://johankanngard.net/
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}

function createTabs() {
    if ($('#offerlist').length > 0) {
        var catlist = '';
        var li = '';
        //var list = '';
        $('#offerlist > li').each(function() {
            catlist += $(this).find('.category').html() + '|';
        })
        var catarr = catlist.split('|');
        catarr.pop();
        catarr = unique(catarr);
        //console.log(catarr);
        $.each(catarr, function(i, val) {
            li += '<li>' + val + '</li>';
        })
        $('#offerlist').before($('<ul id="offertabs">' + li + '</ul>'));
        
        $('#offertabs li').click(function() {
            var tabname = $(this).html();
            $('#offertabs li').removeClass('active');
            $(this).addClass('active');
            $('#offerlist li').each(function() {
                if (tabname == $(this).find('.category').html()) {
                    $(this).fadeIn('fast');
                } else {
                    $(this).fadeOut('fast');
                }
                
            })
        })
    }
}

function googleMap() {
    if (GBrowserIsCompatible()) {
        var map = document.getElementById('map');
        if (map) {
            var info = new Array();
			info[0] = '<div style="width:270px; color: #000;"><strong>CUBE</strong><br /> ul. Kopernika 4/5 <br /> 43-100 Tychy</div>';

			var m = new GMap2(map);
            m.setCenter(new GLatLng(50.1191226,18.9781168), 15);
            m.addControl(new GLargeMapControl());
       		m.addControl(new GMapTypeControl());
       		m.enableDoubleClickZoom();

       		function createMarker(point, number) {
				var marker = new GMarker(point);
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(number);
				});
				return marker;
			}

			var point = new GLatLng(50.1191226,18.9781168);
			m.addOverlay(createMarker(point, info[0]));
        }
    }
}

