var rel = 0;
var thumbsshow = false;
jQuery(function($){
    $('#header').disableSelection();
    //Hauptnavigation Hover Div erstellen und bei Bedarf einblenden
    $('#header ul li:not(.active)').prepend($('<div class="hover"></div>').css('opacity', 0))
    .mouseenter(function(){
        $(this).find('div.hover').clearQueue().animate({opacity: 1}).end().find('a').clearQueue().animate({'color': '#fff'});
    }).mouseleave(function(){
        $(this).find('div.hover').clearQueue().animate({opacity: 0}).end().find('a').clearQueue().animate({'color': '#11395d'});
    });
    
    //Logo als Backlink
    $('#header ul').click(function(e){
        if(e.target == this) document.location.href = '';
    });
    
  
    var currhref = document.location.href.match(/.*$/i)[0].toLowerCase();
    
    var t = currhref.match(/#(.*)$/);
    if(t){
        $('#content > div.pagecontent').remove();
        $.ajax({
            url: '' + t[1],
            contentType: 'text',
            success: function(q){
                var r = q.split('<!-- content -->');
                if(r.length == 3){
                    var id = t[1].match(/idcat=(\d+)/i);
			id = t[1];
                    if(id)
                    $('#header ul li').removeClass('active').find('a').each(function(){
                        if($(this).attr('href') == '/'+id)
                            $(this).parent().addClass('active');
                    });



                    $(r[1]).prependTo($('#content')).css({
                        left: $('#content').width() / 2
                    });
                    pagecontent();
               }
           }
       });
    }
    function pagecontent (){
	$('html,body').animate({scrollTop:0},400,function(){});

        if($('body').height() < $('div.pagecontent').outerHeight())
        {
		if ($.browser.msie && parseInt($.browser.version) < 8) {
			//$('body, #content, div.pagecontent,.content').height(parseInt($('div.pagecontent').outerHeight()));
		} else {
            		//$('body, #content').height($('div.pagecontent').outerHeight());
		}
		$('body, #content').height($('div.pagecontent').outerHeight());
	

        } else {
		if (parseInt($('div.pagecontent').outerHeight()) > 900) {
			//$('body, #content').height($('div.pagecontent').outerHeight());
		} else {
			//$('body, #content').height(900);
		}
		if (parseInt($('div.pagecontent').outerHeight()) < parseInt($('body').height())) {
			$('body, #content').height(parseInt($('body').height()) + 40);
		} else {
			$('body, #content').height(parseInt($('div.pagecontent').outerHeight())+40);
		}
		//$('body, #content').height($('div.pagecontent').outerHeight());
	}
        $('div.pagecontent div.projekte.container.accord').each(function(){


            var $this = $(this);
            $this.find('div.box').click(function(){
                if(!$(this).hasClass('rised'))
                {
			$('body, #content').height(900);
                    var img = $(this).parent().find('div.box').css('display', 'none').end().end()
                              .css('display', 'block').addClass('rised').find('img.rise');
                    img.attr('src', 'img/?550b400' + img.attr('src').match(/_.*$/)[0]);
                    $('<p class="close">Zur&uuml;ck</p>').appendTo(this);
                    
                }else{
			
                    var img = $(this).removeClass('rised').parent().find('div.box').css('display', 'block').end().end().find('img.rise');
                    img.attr('src', 'img/?164b120' + img.attr('src').match(/_.*$/)[0]);
                    $(this).find('p.close').remove();
			$('body, #content').height($('div.pagecontent').outerHeight());
                }
                
            });
        });
        //LINKS
        //var reg = /^(http:\/\/prautec\.ideen-wachstum\.de\/cms\/)?(front_content\.php.*)$/i;
	var reg = /^(http:\/\/www\.prautec\.de\/)(.*)$/i;
	var url_reg = /^(http:\/\/www\.prautec\.de\/)(.*)$/i;
	var url_r = window.location.href.match(url_reg);
        $('a:not(.checked)').addClass('checked').each(function(){
            var r = this.href.match(reg);
            if(r && url_r){
                var href = r[2].toLowerCase();
                $(this).click(function(e){
                   e.preventDefault();
                   if(href == currhref) return;
                   currhref = href;
                   var $this = $(this);
                   $.ajax({
                       url: href,
                       contentType: 'text',
                       success: function(q){
                           var left = true;
                           var speed = 1000;
                           var r = q.split('<!-- content -->');
                           if(r.length == 3){
                               if($this.closest('#header ul li')){
                                   var c = 0;
                                   var t = 0;
                                   var p = $this.parent()[0];
                                   $('#header ul li').each(function(_c){
                                       if($(this).hasClass('active')) c = _c;
                                       if(this === p) t = _c;
                                   }).removeClass('active');
                                   $this.parent().addClass('active');
                                   if(t > c) left = false;

                               }
                               
                               $('#content > div.pagecontent').animate({
                                   'left' : (left ? -490 : $('#content').width() + 490)
                               }, speed, function(){
                                   $(this).remove();
                                   var h = document.location.href.match(/(.*?)(#|$)/);
                                   var g = href.match(/(.*)$/i);
                                   var q = g ? g[1] : '';
                                   document.location.href = h[1] + '#' + q;

                if ($.browser.msie && parseInt($.browser.version) < 8) {
			if (parseInt($('#content').height()) < parseInt($('div.pagecontent').outerHeight())) {
				$('#content').css('height',$('div.pagecontent').outerHeight());
			}
                        //$('#content').css('height','+=1');
			//alert($('#content').height());
                } 


                                 

                               });
                               $(r[1]).prependTo($('#content')).css({
                                   left: (left ? $('#content').width() + 490 : -490)
                               }).animate({
                                   left: $('#content').width() / 2
                               }, speed);
                               pagecontent();
                               
                                                                  
                               
                           }
                       }
                   });
                       
               
               });
            }
        })
    //Aus / Einfahren der Bereiche
        $('#content > div.pagecontent > div.accord > h1').each(function(i){
            var col   = ['white', 'white', 'white'],
                $this = $(this);
            $this.addClass(col[i]);
            $this.click(function(){
                if($this.data('display'))
                {
                    //verstecken
                    $this.data('display', false).parent().find('> div.content').slideUp(function(){
			$('body, #content').height($('div.pagecontent').outerHeight());
		    }).end();
                }else{
                    //zeigen
                    $('#content div.accord').find('> h1').data('display', false).end().find('> .content').slideUp();
			$('body, #content').height(900);
                    $this.data('display', true).parent().find('> div.content').slideDown(function(){
			$('body, #content').height($('div.pagecontent').outerHeight());
		    }).end();
                }
            });

        });
	if ($('#content > div.pagecontent > div.accord > h1').html()) {
		$this = $('#content > div.pagecontent > div.accord > h1:first');
             	$this.ready(function(){
                    //zeigen
                        $('body, #content').height(900);
                    	$this.data('display', true).parent().find('> div.content').slideDown(function(){
                        	$('body, #content').height($('div.pagecontent').outerHeight());
                    	}).end();
            });
	}
    
        //Hinweis bei Mouseover erstellen und positionieren
        $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.kunde').click(function(){
        $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.hint').remove();
        var c = Math.ceil($('#content > div.pagecontent > div.kunden > div.content > div.raster > div.kunde').removeClass('active top').length / 5);
        var $this = $(this);
        var p = $this.position();
        var w = $this.addClass('active').find('.hint').clone().appendTo($this.parent()).outerWidth();
        var a = 1;
        var l = Math.floor(p.left / 163);
        var t = Math.floor(p.top / 163);
        while(a * 163 - 3 < w) a++;
        var hint = $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.hint');
        hint.css({
            width: a * 163 - 3 - hint.outerWidth() + hint.width(),
            left: (l + a > 5 ? (p.left - (l + a - 5) * 163) : p.left),
            top: (t + 1 == c ? p.top - 163 : p.top + 163)
        });
	if ($.browser.msie && parseInt($.browser.version) < 8) {
		hint.css('top','-=163');
	}
        if(t + 1 == c) $this.addClass('top');
        $this.parent().data('hint', true);
        
    }).mouseenter(function(){
        if(!$(this).hasClass('active')){
            $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.kunde').removeClass('active top');
            $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.hint').remove();
        }
    });
    $('#content > div.pagecontent div.content > div.raster > div.kunde:first-child').parent().mouseover(function(e){

       if(e.target !== this) return;
       $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.kunde').removeClass('active top');
       $('#content > div.pagecontent > div.kunden > div.content > div.raster > div.hint').remove();
    });
    
    //Beim Verlassen der Felder Hinweis entfernen
    $('#content > div.pagecontent > div.kunden > div.content > div.raster').mousemove(function(e){
        if(e.target == this && $(this).data('hint')){
            $('#content > div.kunden > div.content > div.raster > div.kunde').removeClass('active top');
            $('#content > div.kunden > div.content > div.raster > div.hint').remove();
            $(this).data('hint', false);
        }
    });
    
    //Lightbox
    $('a.lightbox').lightBox();
    $('#kontakt > div.label.text, #kontakt > div.label.textarea').each(function(){
        var $this = $(this);
        var $input = $this.find('input, textarea').data('hint', $this.text());

        if($input.val().length == 0 || $input.val() == $input.data('hint'))
        {
            $input.addClass('hint').val($input.data('hint'));
        }
        $input.focus(function(){
            var $this = $(this);
            if($this.val() == $this.data('hint'))
                $this.removeClass('hint').val('');
        }).blur(function(){
            var $this = $(this);
            if($this.val() == '')
                $this.addClass('hint').val($this.data('hint'));
        });
    });
    
    //Form bereinigen
    $('#kontakt').parent().find(' > :not(form)').remove();

	if ($('.jobs_table').html()) {
		$('.headarea h1').css('padding-left',51);
		$('.jobs_table TR').mouseenter(function(){
			$(this).find('TD').addClass('jobs_hover');
		});
                $('.jobs_table TR').mouseleave(function(){
                        $(this).find('TD').removeClass('jobs_hover');
                });
	}

	// Protjektseite aufbauen
	if ($('DIV.pagecontent DIV.projekte DIV.content').html()) {
		thumbsshow = false;
		var tmp = $('#projekte_list').find('SPAN');
		if (tmp.length > 2) {
			$('#projekte_list SPAN').css('float','left');
		}
		// als erstes wollen wir die projektueberschriften aus dem orangenem block speichern
		$('#proj_4_1 SPAN').mouseover(function(){
                        var head = $(this).html();
                        $('DIV.projekte DIV.content DIV IMG.proj').each(function(){
				if (!$(this).attr('text')) {
					return;
				}
                                var alt = $(this).attr('text').split('h2>');
                                alt = alt[1].replace(/\<\//,"");
                                if (alt == head) {
					var src = $(this).attr('src');
					if (src.match(/farbflaechen/)) {
						return;
					}
                                        $(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '_h.jpg');
                                }
                        });

		});
		$('#proj_4_1 SPAN').mouseleave(function(){
                        if (thumbsshow) {
                                return;
                        }
                        $('DIV.projekte DIV.content DIV IMG.proj').each(function(){
				var src = $(this).attr('src');
				if (src.match(/farbflaechen/)) {
					return;
				}

                                $(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '.jpg');
                        });
		});
		$('#proj_4_1 SPAN').click(function(){
			var head = $(this).html();
			var html = "";
			var e = false;
			$('DIV.projekte DIV.content DIV IMG.proj').each(function(){
				if (!$(this).attr('text')) {
					return;
				}
                                var alt = $(this).attr('text').split('h2>');
                                alt = alt[1].replace(/\<\//,"");
                                if (alt == head) {
                                        html = $(this).attr('text');
					e = this;
					return false;
                                }
			});
			$('#proj_4_1 #projekte_list').css('display','none');
			$('#proj_4_1 #projekte_content').html(html);
			openThumbshow(e);
			thumbsshow = true;
		});
		$('DIV.projekte DIV.content DIV IMG.proj').mouseenter(function(){
                        if (thumbsshow) {
                                return;
                        }
			var src = $(this).attr('src');
			if (src.match(/farbflaechen/)) {
				return;
			}

			$(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '_h.jpg');
			if ($(this).attr('text')) {
				var $head = $(this).attr('text').split('h2>');
				$head = $head[1].replace(/\<\//,"");
			} else {
				var $head = "";
			}
			/*
			$('#proj_3_1 P').removeClass('orange');
			if ($head) {
				$('#proj_3_1 P').each(function(){
					if ($(this).attr('rel') == $head) {
						$(this).addClass('orange');
					}
				});
			}
			*/
			$('#proj_4_1 #projekte_list').css('display','none');
			$('#proj_4_1 #projekte_content').html($(this).attr('text'));
		});
                $('DIV.projekte DIV.content DIV IMG.proj').mouseleave(function(){
			if (thumbsshow) {
				return;
			}
			var src = $(this).attr('src');
			if (src.match(/farbflaechen/)) {
				return;
			}

                        $(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '.jpg');
			$('#proj_4_1 #projekte_content').html('');
			$('#proj_4_1 #projekte_list').css('display','block');
			//$('#proj_3_1 P').removeClass('orange');
                });
		/*
		$('#proj_3_1 P').mouseenter(function(){
                        if (thumbsshow) {
                                return;
                        }
			$('#proj_3_1 P').removeClass('orange');
			$(this).addClass('orange');
			$('DIV.projekte DIV.content DIV IMG.proj').each(function(){
				$(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '.jpg');
			});
			var head = $(this).attr('rel');
			$('DIV.projekte DIV.content DIV IMG.proj').each(function(){
				var alt = $(this).attr('text').split('h1>');
				alt = alt[1].replace(/\<\//,"");
				if (alt == head) {
					$(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '_h.jpg');
				}
			});
		});
		$('#proj_3_1 P').mouseleave(function(){
                        if (thumbsshow) {
                                return;
                        }

			$('#proj_3_1 P').removeClass('orange');
                        $('DIV.projekte DIV.content DIV IMG.proj').each(function(){
                                $(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '.jpg');
                        });
		});
		*/
		$('#proj_4_1_close').click(function(){
                        var mtop = -153;
                        var ttop = -388;
                        if (rel <= 3) {
                                mtop = -25;
                                ttop = -25;
                        }

			$('#proj_4_1_thumbs_imgs').html("");
			$('#proj_4_1_thumbs').animate({width:436},500).animate({height:0,top:mtop},500,function(){
				$('#proj_4_1_close').css('display','none');
				$('#proj_4_1_thumbs').css('display','none');
				thumbsshow = false;
				$('DIV.projekte DIV.content DIV IMG.proj').each(function(){
                                        var src = $(this).attr('src');
                                        if (src.match(/farbflaechen/)) {
                                                return;
                                        }

					$(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '.jpg');
					$('#proj_4_1').css({'background-color':'#f08e00'});
					$('#proj_4_1 #projekte_content').html("");
					$('#proj_4_1 #projekte_list').css('display','block');
				});
				
			});
			
		});

		$('DIV.projekte DIV.content DIV IMG.proj').click(function(){
			var src = $(this).attr('src');
			if (src.match(/farbflaechen/)) {
				return;
			}

			openThumbshow(this);
		});
		function openThumbshow(e) {
			if ($(e).attr('src').match(/_h\.jpg/) && thumbsshow) {
				// close
				var mtop = -153;
				var ttop = -388;
				if (rel <= 3) {
					mtop = -25;
					ttop = -25;
				}

				$('#proj_4_1_thumbs_imgs').html("");
				$('#proj_4_1_thumbs').animate({width:436},500).animate({height:0,top:mtop},500,function(){
					$('#proj_4_1_close').css('display','none');
					$('#proj_4_1_thumbs').css('display','none');
					thumbsshow = false;
					$('DIV.projekte DIV.content DIV IMG.proj').each(function(){
						var src = $(this).attr('src');
						if (src.match(/farbflaechen/)) {
							return;
						}

						$(this).attr('src','upload/projekte_neu/' + $(this).attr('rel') + '.jpg');
						$('#proj_4_1').css({'background-color':'#f08e00'});
	                                        $('#proj_4_1 #projekte_content').html("");
        	                                $('#proj_4_1 #projekte_list').css('display','block');

					});

				});

			}
                        if (thumbsshow) {
                                return;
                        }
			if (!$(e).attr('text')) {
				return;
			}
			thumbsshow = true;
			$('#proj_4_1_close').css('display','block');
			$('#proj_4_1').css({'background-color':'#003858'});
			var mtop = -153;
			var ttop = -388;
			rel = parseInt($(e).attr('rel'));
			if (rel <= 3) {
				// nach unten
				mtop = -25;
				ttop = -25;
			}
			$('#proj_4_1_thumbs').css({top:mtop});
			$('#proj_4_1_thumbs').css('display','block');
			$('#proj_4_1_thumbs').animate({height:218,top:ttop},500,function(){}).animate({width:945},500,function(){
				$('#proj_4_1_thumbs_imgs').html($('#thumbs_'+rel).html());
				$('#proj_4_1_thumbs_imgs IMG.thumb').click(function(){
					if ($(this).attr('video')) {
						var video = $(this).attr('video');
						video = video.replace(/http:\/\/www\.prautec.de\/cms\//,"");
						//$('#thumb_big').html('<a class="media {width:900, height:560}" href="upload/mediaplayer.swf?file='+video+'&autostart=true"></a><div id="close_video"><img src="upload/projekte_neu/x.png"></div>');

						var deviceAgent = navigator.userAgent.toLowerCase();
						$iOS = deviceAgent.match(/(iphone|ipod|ipad)/);

						var url = window.location.href;
						if (url.match(/m\.prautec\.de/i) && $iOS) {
							video = "/cms/upload/"+video.replace(/\.flv/," - Wi-Fi.m4v");
							$('#thumb_big').html('<div style="background-color: rgb(255, 255, 255); width: 900px;" class="media"><video id="video" autobuffer controls autoplay height="560" width="900"><source src="'+video+'"></video></div><div id="close_video"><img src="upload/projekte_neu/x.png"></div>')
						} else {
							$('#thumb_big').html('<div style="background-color: rgb(255, 255, 255); width: 900px;" class="media"><object type="application/x-shockwave-flash" data="upload/mediaplayer.swf?file='+video+'&amp;autostart=true" width="900" height="560"><param name="src" value="upload/mediaplayer.swf?file='+video+'&amp;autostart=true"><param name="bgColor" value="#ffffff"><param name="wmode" value="transparent"><param name="autoplay" value="false"><param name="flashvars" value=""><div><p><strong>Flash Required</strong></p><p>Flash is required to view this media. <a href="http://www.adobe.com/go/getflashplayer">Download Here</a>.</p></div></object></div><div id="close_video"><img src="upload/projekte_neu/x.png"></div>');
						}
						
						//$('a.media').media();
						$('#thumb_big').css({display:'block'});
						$('#close_video').click(function(){
                                                        $('#thumb_big').css({display:'none'});
                                                        $('#thumb_big').html("");
						});
					} else {
						$('#thumb_big').css({display:'block'});
						$('#thumb_big').html("<img src='"+$(this).attr('src')+"' height='565'><img src='upload/projekte_neu/minus.jpg' class='minus'>");
						$('#thumb_big IMG').click(function(){
							$('#thumb_big').css({display:'none'});
							$('#thumb_big').html("");
						});
					}
				});
				$('#proj_4_1_thumbs_imgs .plus').click(function(){
					var $this = $('#proj_4_1_thumbs_imgs .plus').parent().find('IMG.thumb');
					var $this = $this[0];
                                        $('#thumb_big').css({display:'block'});
                                        $('#thumb_big').html("<img src='"+$($this).attr('src')+"' height='565'><img src='upload/projekte_neu/minus.jpg' class='minus'>");
                                        $('#thumb_big IMG').click(function(){
                                                $('#thumb_big').css({display:'none'});
                                                $('#thumb_big').html("");
                                        });

				});
			});
		}
	}


    };
    pagecontent();
    //Hintergrund
    $(window).resize(function(){

      $('#content').css('height', $(window).height());
      $('body').css('backgroundImage', 'url(img/?' + $(window).width() + 'c'
          + $(window).height() + 'q60_upload/111110_Prautec_Hintergrundmotiv.jpg)');
    }).trigger('resize');
    

});
jQuery(function($){
	$('document').ready(function(){
		if (mobile) {
			// Bei Mobile muessen wir die breite setzten
			/*$('BODY').css('width',1000);
			$('#content').css('overflow','scroll');
			$('#content').css('width',1000);*/
			$('#content').css('min-height',800);
		}
	});
});

