var $$ = document;

var xmlHttps = null;

var ADS = ADS || {};

try {

	if( typeof ADS !== "undefined" ) {

		ADS = {

				options : {

					timeout : 0 // auto-close/collapse timer

				},
				
				init : function(){ console.log("Library loaded"); },

				// e.g. size  [ width , height ]
				starndard : function( id , size , custom ) {

					ADS.send({
						"id" 		: id,
						"style" 	: ( custom ? custom : [{"width":size[0]+"px"},{"height":size[1]+"px"}] ),
						"animate" 	: true
					});

				},

				// e.g. size  [ width , height ]
				richmedia : function( id , name , custom ) {

					ADS.send({
						"id" 		: id,
						"style" 	: ( custom ? custom : ADS._ad_type(name) ),
						"animate" 	: false
					});

				},

				_autoclose : function( id , name , custom ) {

					var t = setTimeout(function(){

						ADS.richmedia( id , name , (custom ? custom : {"display":"none", "visibility":"hidden"}) );

						clearTimeout(t);

					}, ADS.options.timeout)

				},

				_ad_type : function( name ) {

					var type = new Array();


					type["skinning"] 		= {"width":"1600px" , "height":"700px" 	, "position":"fixed" , "top":"0" 	, "left":"50%" 	, "z-index":"0" 			, "margin":"140px 0 0 -800px"};
					type["skinning_2x120"] 	= {"width":"1280px" , "height":"600px" 	, "position":"fixed" , "top":"0" 	, "left":"50%" 	, "z-index":"0" 			, "margin":"140px 0 0 -640px"};
					type["interstitial"]	= {"width":"100%" 	, "height":"100%" 	, "position":"fixed" , "top":"0" 	, "left":"0" 	, "z-index":"100" 			, "background-color":"rgba(0,0,0,0.8)"};
					type["border"] 			= {"width":"970px" 	, "height":"90px" 	, "position":"fixed" , "bottom":"0" , "left":"50%" 	, "margin":"0 0 0 -490px" 	, "z-index":"99"};

					console.log(type["skinning"]);

					return type[ name ];

				},

				_resize : function( id , size , animate ) {

					ADS.send({
						"id" 		: id,
						"style" 	: { "width":size[0] , "height":size[1] },
						"animate" 	: animate ? animate : false
					});

				},

				_position : function( size ) {

					return Math.ceil( parseInt(size/2) * -1 );

				},

				_event : function( id , name , user_func , upper_level ){

					$$.getElementById( id ).addEventListener( name , user_func , false);

				},

				send : function( attr ) {

					window.parent.postMessage( attr , "*" );

				},

				receive : function() {

					window.addEventListener("message", function( e ){

						if( typeof e.data.id !== "undefined" && typeof e.data.style !== "undefined" ) {

							var count 	= parseInt(e.data.style.length); // count styles
							
							var ID 		= e.data.id; // get element ID
							
							var data 	= e.data.style; // get new style

							if( count > 1 ) {
								
								// apply multiple styles
								for( i=0; i<count; i++ ) {
									var key = Object.keys( data[i] );
									$$.getElementById( ID ).getElementsByTagName("iframe")[0].style[key] = data[i][key];
								}

							} else {

								// resize with animation
								if( typeof e.data.animate !== "undefined" ) {

									if( e.data.animate === true ) {
										jQuery("#"+ID).find("iframe").stop().animate( data , 400 , function(){
											console.log("completed");
										});
									} else {
										jQuery("#"+ID).find("iframe").stop().css( data );
									}

								// apply single style
								} else {
									var key = Object.keys( data[0] );
									$$.getElementById( ID ).getElementsByTagName("iframe")[0].style[key] = data[key];	
								}
							}

						}

					}, false);

				}


		};

		$(window).on("scroll", function(){

			var $$window 			= $(window);
			var $$currentScroll 	= $$window.scrollTop() || 0;
			var $$skinning_wrapper	= $("#skinning");
			var $$skinning_ad 		= $$skinning_wrapper.find("iframe");
			var $$leaderboard_ad 	= $("#strip_ad");
			var $$offset_top		= (typeof $$leaderboard_ad.offset() !== "undefined" ? $$leaderboard_ad.offset().top : 0) ;

			// skinning ad
			if( $$currentScroll > $$offset_top ) {
				$$skinning_ad.css({"margin-top":"0" });
			} else {
				$$skinning_ad.css({"margin-top": Math.abs( $$offset_top - $$currentScroll) + "px"});
			}

		});

	} else console.log( "Cannot find the master variable" );

} catch(e) {

	console.log( "Cannot load the library" );

}