/* ALLE $ müssen mit $scw ersetzt werden! CompatMode wegen Lightbox! */
// alert("I'm running jQuery");

var $scw = jQuery.noConflict();

$scw(document).ready(function() {  
		
	//set variables
	var amountWideTeaser = 0;
	var amountSmallTeaser = 0;
	var amountTeaser = 0;
	
	var i=1;
	$scw(".scwGlobalFrameSmallContent").each(function(){ //counts teaser areas
       	$scw(this).attr('id','scwGlobalFrameSmallContent'+i);
       	amountSmallTeaser = i;
    	i++;
    });
    
    var i=1;
	$scw(".scwFrameWide").each(function(){ //counts main areas
       	$scw(this).attr('id','scwFrameWide'+i);
       	amountWideTeaser = i;
    	i++;
    });
	
    if(amountSmallTeaser != amountWideTeaser){ //control for the right proportion of elements
    	alert("Check the right amount of Big Elements and Frames for small Elements in Teaser, otherwise scripts won't work!");
    }else{
    	amountTeaser = amountSmallTeaser;
    }
    
    //set height of boxes
    var i=1;
	for (i=1;i<=amountTeaser;i++){
		var sizeLeft = $scw("#scwFrameWide"+i).height(); //get window hight for definied boxes
		var sizeRight = $scw("#scwGlobalFrameSmallContent"+i).height(); //get window hight for definied boxes
		
		var j=0;
		$scw("#scwGlobalFrameSmallContent"+i+" .scwFrameSmallIndex").each(function(){ //counts the single elements in the right teaser
	       	j++;
	    });
	    
	    var margin = 13;
	    var spaceBox = margin*j; //definition of all margin between small boxes
	    
	    if ($scw.browser.msie) {
		    var padding = 0; //sum of padding top and bottom paddings of coloured boxes
		}else{
		    var padding = 60; //sum of padding top and bottom paddings of coloured boxes
		}
	    
	    //var padding = 60; //sum of padding top and bottom paddings of coloured boxes
		var paddingBox = padding*j; //definition of padding top and bottom of background boxes
		var addSize = 0;
		var fallBack = 0;
		var sizeRightSingle = 0;
		
		if(sizeLeft > sizeRight){ //compares the height of the containers and sets height new
			var rightBoxes = (sizeLeft - paddingBox - spaceBox) / j; //definition of the single boxes when left > right
			$scw("#scwGlobalFrameSmallContent"+i+" .scwFrameSmallIndex").each(function(){
		    	sizeRightSingle = $scw(this).height(); //height of current right element
		    	if(sizeRightSingle>rightBoxes){ //compares current height with calculated from the left column
		    		fallBack = 1;
		    		addSize = sizeRightSingle - rightBoxes;
		    		$scw(this).css("height", rightBoxes + addSize);
		    	}else{
		    		$scw(this).css("height", rightBoxes);
		    	}
		    });
			if(fallBack == 1){ //resizes left column if recalculation above is given
				var sizeRightTemp = $scw("#scwGlobalFrameSmallContent"+i).height(); //get window hight for definied boxes
				$scw("#scwFrameWide"+i+" .scwFrameWideIndex").css("height", sizeRightTemp - margin - padding);
			}
		}else if(sizeLeft < sizeRight){	
			$scw("#scwFrameWide"+i+" .scwFrameWideIndex").css("height", sizeRight - margin - padding);
		}
	}
});
