$(document).ready(function() {
  Util.setMaps();
  Util.setSameHeights();
  Scroll.init();
  Gallery.init();
  Start.init();
  Accordion.init();

  if($("body").attr("class").match("prepage")) {
    var dimensions = Util.getWindowSize();
    $("#prepage").height(dimensions[1]);
  }
  
  //--- Lightbox ---//
  if($(".lightbox")) {
    $(".lightbox").lightbox();
  }
});

Scroll = {
  left: 0,
  width: 0,
  itemWidth: 0,
  interrupt: false,
	
  init: function() {
	$("#brand_list li").each(function() {
		Scroll.width+= parseInt($(this).width())+parseInt($(this).css("margin-left"))+parseInt($(this).css("margin-right"));
	});
	$("#brand_list .sledge").css("width", (Scroll.width*2));
	$("#brand_list .sledge li").each(function() {
		$(this).clone().insertAfter("#brand_list .sledge li:last");
	});
    Scroll.run();
    $("#brand_list").hover(function() {
      Scroll.interrupt= true;
    }, function() {
      Scroll.interrupt= false;
    });
  },
  run: function() {
    if(!Scroll.interrupt) {
      if((Scroll.left*-1)>=Scroll.width) {
		Scroll.left = 0;
		$("#brand_list .sledge").animate({
	      marginLeft: Scroll.left
	    }, 0);
      }
      Scroll.left-=10;
    }
    $("#brand_list .sledge").animate({
      marginLeft: Scroll.left
    }, 150, "linear", function() {
      Scroll.run();
    });
  }
}

Accordion = {
	init: function() {
		if($("#brandlist")) {
			$(".brand_category").each(function() {
				$(this).removeClass("nolink_brand").addClass("inactive_brand");
			}).click(function() {
				Accordion.open($(this))
			});
			$(".brand_category:first").removeClass("inactive_brand").addClass("active_brand");
			$(".subbrandlist").each(function() {
				$(this).css("height", Math.ceil($(".brand_item", $(this)).size()/4)*100).hide();
			});
			$(".subbrandlist:first").show();
		}
	},
	open: function(obj) {
		if(obj.attr("class").match("active_brand") && !obj.attr("class").match("inactive_brand")) {
			return;
		}
		old= $(".active_brand").next();
		old_height= old.css("height");
		old.animate({
	      height: 0
	    }, 150, "swing", function() {
			$(this).hide().css("height", old_height);
		});
		$(".active_brand").removeClass("active_brand").addClass("inactive_brand");
	
		obj.removeClass("inactive_brand").addClass("active_brand");
		mynew= obj.next();
		new_height= mynew.css("height");
		mynew.css("height", 0).show();
		mynew.animate({
	      height: new_height
	    }, 150, "swing");
	}
}

Start = {
	current: 1,
	max: 0,
	
	init: function() {
		Start.max = $("#startimagecount").html();
		window.setTimeout(Start.run, 5000);
	},
	run: function() {
		$("#startimage_current").fadeOut("slow", function() {
			$("#startimage_current").html($("#startimage_next").html()).show();
			next = Start.current+1;
			if(next == Start.max) {
				next = 0;
			}
			$.ajax({
				url: '/gallery/'+next,
				type: 'GET',
				dataType: 'html',
				timeout: 5000,
				success: function(html) {
					if(html!="") {
						$("#startimage_next").html(html);

						Start.current = next;
						window.setTimeout(Start.run, 5000);
					}
				}
			});
		});
	}
}

Gallery = {
	width: 740,
	current: 0,
	max: 0,
	
	init: function() {
		$("#galleryback, #gallerynext").hover(function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
		});
		$("#gallerynext").show().click(function() {
			Gallery.move(1);
		});
		$("#galleryback").click(function() {
			Gallery.move(-1);
		});
		Gallery.max = $("#content .gallery .sledge .imageframe").size();
		$("#content .gallery .sledge").css("width", Gallery.width*Gallery.max);

		window.setTimeout(Gallery.run, 5000);
	},
	run: function() {
		if(Gallery.current<Gallery.max-1) {
			Gallery.move(1);
			window.setTimeout(Gallery.run, 5000);
		}
	},
	move: function(direction) {
		Gallery.current+= direction;
		if(Gallery.current==0) {
			$("#galleryback").hide();
		} else {
			$("#galleryback").show();
		}
		if(Gallery.current==Gallery.max-1) {
			$("#gallerynext").hide();
		} else {
			$("#gallerynext").show();
		}
		left= Gallery.width*Gallery.current*-1;
		$("#content .gallery .sledge").animate({
	      left: left
	    }, 200);
	}
}

Util = {
  setMaps: function() {
    //$("#google_map_static").load(function () {
      var address = $("#google_map_static").attr('alt');
      var iframe = '<iframe id="google_map_dynamic" width="740" height="251" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?source=s_q&amp;hl=de&amp;zoom=16&amp;q=' + address + '&amp;output=embed'
      + "&amp;iwloc=pause1" +
      '"></iframe>';
      $("#google_map_static").parent().html(iframe);
    //});
  },
  externalLinks: function() {
    //Source: http://www.sitepoint.com/print/standards-compliant-world/
    if (!document.getElementsByTagName) {
      return;
    }
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
      var anchor = anchors[i];
      if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
        anchor.target = "_blank";
      }
    }
  },
  getWindowSize: function(){
    var xScroll, yScroll;

    if (window.innerHeight && window.scrollMaxY) {
      xScroll = window.innerWidth + window.scrollMaxX;
      yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) {
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
    } else if(document.body) {
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
    }

    var windowWidth, windowHeight;
    if (self.innerHeight) {
      if(document.documentElement.clientWidth){
        windowWidth = document.documentElement.clientWidth;
      } else {
        windowWidth = self.innerWidth;
      }
      windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
    } else if (document.body) {
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
    }

    if(yScroll < windowHeight) {
      pageHeight = windowHeight;
    } else {
      pageHeight = yScroll;
    }

    if(xScroll < windowWidth) {
      pageWidth = xScroll;
    } else {
      pageWidth = windowWidth;
    }

    var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
    return arrayPageSize;
  },
  setSameHeights: function() {
	var max= 0;
	$(".sameheight").each(function() {
	  if(max < parseInt($(this).height())+parseInt($(this).css("padding-top"))+parseInt($(this).css("padding-bottom"))) {
		max = parseInt($(this).height())+parseInt($(this).css("padding-top"))+parseInt($(this).css("padding-bottom"));
	  }
	}).each(function() {
	  personalMax= max-parseInt($(this).css("padding-top"))-parseInt($(this).css("padding-bottom"));
	  $(this).css("height", personalMax);
	});
  },
  validateForm: function(id) {
		var result= true;
		$("#"+id + " input, #"+id + " textarea").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			  // trim input field
			  $(this).val($(this).val().replace(/^\s+|\s+$/g, ''));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});

		if(result) {
			$("#form_error").removeClass("error");	
		} else {
			$("#form_error").addClass("error");
		}

		return result;
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null )
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");
		}
		return result;
	}

}

window.onload = Util.externalLinks;
