var isIOS = navigator.userAgent.match(/iPhone/i) !== null || navigator.userAgent.match(/iPad/i) !== null || navigator.userAgent.match(/iPod/i) !== null;
var bereitsBetaetigt = false;
$(document).ready(function() {
   // Leiste autom. hereinfahren
   setTimeout(function(){
      if (bereitsBetaetigt == false) {
         $(".mediascroller .arrowright").trigger('click');
         bereitsBetaetigt = true;
      }
   }, 5000);
   
   
              //var max_width_abzug_firstclick = 51;
	var MediaScroller = new Array();
	$(".mediascroller").each(function(i) {
      var minha = $(this).children(".scrollcontainer").children(".mediainhalt");
      MediaScroller[i] = new Array();
      MediaScroller[i]["max_width"] = $(this).children(".scrollcontainer").css("width");
      MediaScroller[i]["max_width"] = parseInt(MediaScroller[i]["max_width"]);
      MediaScroller[i]["firstscroll_clicked"] = false;
      MediaScroller[i]["width"] = 0;
      MediaScroller[i]["left"] = 0;
      
      //MediaScroller[i]["aleftspecial"] = 0;
      MediaScroller[i]["aleftspecial"] = 1;
      
      
      $(minha).find("a").each(function(y) {
      	MediaScroller[i]["width"] += $(this).children("img").width() + parseInt($(this).children("img").css("borderLeftWidth")) + parseInt($(this).children("img").css("borderRightWidth"));
      });
      $(minha).css("width", MediaScroller[i]["width"]+"px")
      $(this).children(".arrowright").click(function() {
         bereitsBetaetigt = true;
         
         //MediaScroller[i]["aleftspecial"] = 0;
         MediaScroller[i]["aleftspecial"] = 1;
         if (MediaScroller[i]["firstscroll_clicked"] == false) {
            MediaScroller[i]["firstscroll_clicked"] = true;
            //max_width_abzug_firstclick = 0;
            //$(this).parent().children(".scrollcontainer").children(".mediainhalt").animate({ left: "-=885px" }, 'slow', 'swing');
            $(this).parent().children(".scrollcontainer").children(".mediainhalt").animate({ left: "-=790px" }, 'slow', 'swing');
            $(".arrowleft").fadeIn();
            $(".arrowleft").css('cursor', 'pointer');
            //$(".arrowleft").css('display', 'block');
         } else {
             $(".arrowleft").fadeTo('fast', 1);
             $(".arrowleft").css('cursor', 'pointer');
      	var arrowwidth = $(this).width();
               arrowwidth = 1;
      	//var toscroll = MediaScroller[i]["max_width"]-(2*arrowwidth);
      	var toscroll = MediaScroller[i]["max_width"]-(2*arrowwidth)-95;
      	var newpos = MediaScroller[i]["left"] + toscroll;
        //alert('right: '+toscroll+" | "+newpos);
      	//if (newpos > ((MediaScroller[i]["width"]-(2*arrowwidth)+95) - toscroll)) {
      	if (newpos > ((MediaScroller[i]["width"]-(2*arrowwidth)+95) - toscroll)) {
      	//	newpos = (MediaScroller[i]["width"]-(2*arrowwidth)+95) - toscroll;
      		newpos = (MediaScroller[i]["width"]-(2*arrowwidth)+95) - toscroll;
                        $(".arrowright").fadeTo('fast', 0.4);
                        $(".arrowright").css('cursor', 'default');
                       
      	}
      	var chgpos = MediaScroller[i]["left"] - newpos;
      	if (chgpos != 0) {
      		$(this).parent().children(".scrollcontainer").children(".mediainhalt").animate({ left: "+="+chgpos+"px" }, 'slow', 'swing');
      	}
      	MediaScroller[i]["left"] = newpos;
         }
        /*
        if (MediaScroller[i]["firstscroll_clicked"] == false) {
            MediaScroller[i]["firstscroll_clicked"] = true;
            max_width_abzug_firstclick = 0;
         }
         */
      });
      $(this).children(".arrowleft").click(function() {
         bereitsBetaetigt = true;
         
         if (MediaScroller[i]["firstscroll_clicked"] == false) {
            //MediaScroller[i]["firstscroll_clicked"] = true;
            //max_width_abzug_firstclick = 0;
            //$(this).parent().children(".scrollcontainer").children(".mediainhalt").animate({ left: "=0px" }, 'slow', 'swing');
            //$(".arrowleft").css('display', 'block');
         } else {
            var tostartsetup = 0;
            $(".arrowright").fadeTo('fast', 1);
            $(".arrowright").css('cursor', 'pointer');
      	var arrowwidth = $(this).width();
               arrowwidth = 1;
      	//var toscroll = MediaScroller[i]["max_width"]-(2*arrowwidth);
      	var toscroll = MediaScroller[i]["max_width"]-(2*arrowwidth)-95;
      	var newpos = MediaScroller[i]["left"] - toscroll;
        //alert('left: '+toscroll+" | "+newpos);
        /*
      	if (newpos < (0-arrowwidth)) {
      		newpos = (0-arrowwidth);
      	}
        */
               
               //if (newpos < (-95)) {
               if (newpos <= (-883)) {
      		newpos = (-95);
                
                if (MediaScroller[i]["aleftspecial"] == 1) {
                     tostartsetup = 1;
                     //newpos = -885;
                     newpos = -790;
                     $(".arrowleft").css('cursor', 'default');
                     $(".arrowleft").fadeOut();
                     
                } else if (MediaScroller[i]["aleftspecial"] == 0) {
                   MediaScroller[i]["aleftspecial"] = 1;
                }
                
                //$(".arrowleft").fadeTo('fast', 0.4);
                //$(".arrowleft").css('cursor', 'default');
                
      	}else if (newpos <0) {
                  newpos = 0;
               }
      	var chgpos = MediaScroller[i]["left"] - newpos;
      	if (chgpos != 0) {
      		$(this).parent().children(".scrollcontainer").children(".mediainhalt").animate({ left: "+="+chgpos+"px" }, 'slow', 'swing');
      	}
      	MediaScroller[i]["left"] = newpos;
        
               if (tostartsetup == 1) {
                  MediaScroller[i]["firstscroll_clicked"] = false;
                     MediaScroller[i]["left"] = 0;
                     //MediaScroller[i]["aleftspecial"] = 0;
                     MediaScroller[i]["aleftspecial"] = 1;
                     tostartsetup = 0;
               }
         }
      });
   });
   
   if (isIOS) {
        $(".scrollcontainer").swipe({swipe:swipeMediaScroller, triggerOnTouchEnd: false, allowPageScroll: 'vertical', threshold:75 });
   }
   function swipeMediaScroller(event, direction) {
       if (direction == 'left') {
           $(".mediascroller > .arrowright").trigger('click');
       } else if (direction == 'right') {
           $(".mediascroller > .arrowleft").trigger('click');
       }
   }
});
