function copyOpeningHours() {
    var from_a = $('#mon_a_from').val();
    var till_a = $('#mon_a_till').val();
    var from_b = $('#mon_b_from').val();
    var till_b = $('#mon_b_till').val();
    
    $('input.opening-hours').each(function() {
       if ($(this).attr('name').indexOf('a_from') > 0) {
            $(this).val(from_a);
       }
       else if ($(this).attr('name').indexOf('a_till') > 0) {
            $(this).val(till_a);
       }
       else if ($(this).attr('name').indexOf('b_from') > 0) {
            $(this).val(from_b);
       }
       else if ($(this).attr('name').indexOf('b_till') > 0) {
            $(this).val(till_b);
       }
    });
}

function sendCompanyContact() {
	$.post("/ajax/company-contact", $("#contact-company").serialize(), function(data) {
		$('ul.errors').remove();
		if (data.errors) {
			if (data.errors.your_name)
				$('#your-name').after('<ul class="small errors"><li>'+data.errors.your_name+'</li></ul>');
			if (data.errors.your_email)
				$('#your-email').after('<ul class="small errors"><li>'+data.errors.your_email+'</li></ul>');
			if (data.errors.txt)
				$('#txt').after('<ul class="small errors"><li>'+data.errors.txt+'</li></ul>');
			if (data.errors.captcha)
				$('span.small').after('<ul class="small errors"><li>'+data.errors.captcha+'</li></ul>');
		}
		else if (data.success) {
			$('#contact-company').slideUp(500).after('<div class="success">'+data.success+'</div>');
		}
	}, 'json');
}

function sendClaimContact() {
	$.post("/ajax/company-claim", $("#contact-company").serialize(), function(data) {
		$('ul.errors').remove();
		if (data.errors) {
			if (data.errors.your_name)
				$('#your-name').after('<ul class="small errors"><li>'+data.errors.your_name+'</li></ul>');
			if (data.errors.your_email)
				$('#your-email').after('<ul class="small errors"><li>'+data.errors.your_email+'</li></ul>');
			if (data.errors.txt)
				$('#txt').after('<ul class="small errors"><li>'+data.errors.txt+'</li></ul>');
			if (data.errors.captcha)
				$('span.small').after('<ul class="small errors"><li>'+data.errors.captcha+'</li></ul>');
		}
		else if (data.success) {
			$('#contact-company').slideUp(500).after('<div class="success">'+data.success+'</div>');
		}
	}, 'json');
}

function showFooter() {
	 var cookie_name = 'addyourcompany';
     var options = { path: '/', expires: 2 };
     var count = ($.cookie(cookie_name)) ? parseInt($.cookie(cookie_name)) : 0;
     var rand = Math.ceil(100*Math.random());
     
     if (count < 40) {
    	 if (rand % 2 || count <= 0)
    		 $('#add-your-company').slideDown(500);
  		 $.cookie(cookie_name, ++count, options);
     }
}

function createMarker(map, point, index, addr_id, company, address, zipcode, city, link, logo) {
   //  var letter = String.fromCharCode("A".charCodeAt(0) + index);
    var letteredIcon = new GIcon(baseIcon);
    letteredIcon.image = "http://www.google.com/mapfiles/marker.png";

    if (logo) {
		var companyhtml = "<h3><a href='"+link+"' class='heading'>"+company+"</a></h3>";
		companyhtml += '<a href="'+link+'" class="fright"><img src="http://img.foxx.be/cache/50/40/r/img/'+logo+'" /></a>';
    	        	
    }
    else
        var companyhtml = "<h3><a href='"+link+"' class='heading'>"+company+"</a></h3>";

    companyhtml += address+"<br />";
    companyhtml += zipcode+" "+city+"<br />";

    markerOptions = { icon:letteredIcon };
    var marker = new GMarker(point, markerOptions);

    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(companyhtml);
    });
    $('#addr-'+addr_id).click(function(){
    	map.panTo(marker.getLatLng()); 
  	  window.setTimeout(function(){marker.openInfoWindowHtml(companyhtml)},600); 
    });
    $('#addrli-'+addr_id).click(function(){
    	map.panTo(marker.getLatLng()); 
  	  window.setTimeout(function(){marker.openInfoWindowHtml(companyhtml)},600); 
    });
    return marker;
  }

function createSimpleMarker(point, index, company, address, zipcode, city) {
	// var letter = String.fromCharCode("A".charCodeAt(0) + index);
	var letteredIcon = new GIcon(baseIcon);
	letteredIcon.image = "http://www.google.com/mapfiles/marker.png";

	var companyhtml = "<strong>"+company+"</strong><br />";
	companyhtml += address+"<br />";
	companyhtml += zipcode+" "+city+"<br />";
	markerOptions = { icon:letteredIcon };
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(companyhtml);
	});
	return marker;
}

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString();
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { 
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery.fn.highlight = function(pat) {
	 function innerHighlight(node, pat) {
	  var skip = 0;
	  if (node.nodeType == 3) {
	   var pos = node.data.toUpperCase().indexOf(pat);
	   if (pos >= 0) {
	    var spannode = document.createElement('span');
	    spannode.className = 'highlight';
	    var middlebit = node.splitText(pos);
	    var endbit = middlebit.splitText(pat.length);
	    var middleclone = middlebit.cloneNode(true);
	    spannode.appendChild(middleclone);
	    middlebit.parentNode.replaceChild(spannode, middlebit);
	    skip = 1;
	   }
	  }
	  else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
	   for (var i = 0; i < node.childNodes.length; ++i) {
	    i += innerHighlight(node.childNodes[i], pat);
	   }
	  }
	  return skip;
	 }
	 return this.each(function() {
	  innerHighlight(this, pat.toUpperCase());
	 });
	};

	jQuery.fn.removeHighlight = function() {
	 return this.find("span.highlight").each(function() {
	  this.parentNode.firstChild.nodeName;
	  with (this.parentNode) {
	   replaceChild(this.firstChild, this);
	   normalize();
	  }
	 }).end();
	};



$(function() {
	showFooter();
	
	$('#q').focus();
	
	var max = 0;  
	$("label").each(function(){  
		if ($(this).width() > max)  
			max = $(this).width();  
	});
	$("label").width(max+14); 
	$("ul.errors").css('margin-left', (max+14)+'px');
	
	var max = 0;  
	$("dt").each(function(){  
		if ($(this).width() > max)  
			max = $(this).width();  
	});
	var width = (max+14);
	$("dt").width(width); 
	$("#company-label").width('0');
	$("#subm-label").width('0');
	$("#groups-label").width('0');
	$(".period2").css('padding-left', width+'px');
});

