
$(document).ready(eventActivator);

function eventActivator(){
	$('.slider-ctrl').each(slideDetails);
}

function slideDetails(count){
		this.onclick = function(){
		var $currCol = $(this).parent().get(0);
		var $currRow = $($currCol).parent().get(0);
		var $colsCurrRow = $($currRow).children();
		var $currDetailsRow = null;
		var $colCurrDetails = null;
		if(!$($currRow).next().hasClass("coupon-details"))
		{
			$($colsCurrRow).each(function(){
				$(this).toggleClass('no-border');
			})
			$($currRow).after('<tr class="coupon-details"><td class="brdr"></td><td class="slide-container" colspan="4"><div class="slide-div"><img class="loading" src="/coupons/all/images/ajax-loader.gif" width="220" height="19" border="0" /></div></td></tr>');
			if($($currRow).next().hasClass("coupon-details"))
			{
				$currDetailsRow = $($currRow).next().get(0);
				$colCurrDetails = $($currDetailsRow).children();
				
				for(i=0; i<$colsCurrRow.length; i++)
				{
					if($($colsCurrRow[i]).children().length > 0)
					{
						if($($colsCurrRow[i]).children()[0].tagName == "IMG" && $($($colsCurrRow[i]).children()[0]).hasClass("slider-ctrl"))
						{
							var imgArrow = $($colsCurrRow[i]).children()[0];
							$(imgArrow).attr("src","/coupons/all/images/header_on.png");
						}
					}
				}
				
				if($($colsCurrRow[0]).hasClass("cash-back"))
				{
					$($colCurrDetails[0]).addClass("cash-back");
				}
				else if($($colsCurrRow[0]).hasClass("coupons-cash-back"))
				{
					$($colCurrDetails[0]).addClass("coupons-cash-back");
				}
				for(i=0; i<$colCurrDetails.length; i++)
				{
					if($($colCurrDetails[i]).children().length > 0)
					{
						if($($colCurrDetails[i]).children()[0].tagName == "DIV" && $($($colCurrDetails[i]).children()[0]).hasClass("slide-div"))
						{
							var $foundContainer = $($colCurrDetails[i]).children()[0];
							$($colCurrDetails[i]).css('vertical-align','top');
							$($foundContainer).css('text-align','left');
							$($foundContainer).load('/coupons/detail/index.htm?coupon_id='+$($currRow).attr("id"));
						}
					}
				}
			}
		}
		else
		{
			if($($currRow).next().hasClass("coupon-details"))
			{
				$currDetailsRow = $($currRow).next().get(0);
				$($currDetailsRow).toggle();
				$($colsCurrRow).each(function(){
					$(this).toggleClass('no-border');
				})
				//alert(imgArrow);
				for(i=0; i<$colsCurrRow.length; i++)
				{
					if($($colsCurrRow[i]).children().length > 0)
					{
						if($($colsCurrRow[i]).children()[0].tagName == "IMG" && $($($colsCurrRow[i]).children()[0]).hasClass("slider-ctrl"))
						{
							var imgArrow = $($colsCurrRow[i]).children()[0];
							if($(imgArrow).attr("src")=="/coupons/all/images/header_on.png")
								$(imgArrow).attr("src","/coupons/all/images/header_off.png");
							else
								$(imgArrow).attr("src","/coupons/all/images/header_on.png");
						}
					}
				}
			}
		}
	}
}
