jQuery('head').append('<style type="text/css">.javascript, .style_head_src, .style_head_append, .javascript_load, .script_classid, .videopath, .body_wrapper, .form-data, body { display: none; } </style>');

jQuery.showVideo = function(target_id, _prop, _param, _vars) {
	var so = new SWFObject(_prop.player, _prop.label, _prop.width, _prop.height, '9',_prop.background);
	for ( kv in _param ) so.addParam(kv, _param[kv]);
	for ( kv in _vars ) so.addVariable(kv, _vars[kv]);
	so.write(target_id);
};

function isUrl(s) {
	var regexp = new RegExp("^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$", "g");
	return regexp.test(s);
}

function aWeberContlr() {
	jQuery.scriptToEval = "";
	jQuery('div.form-data').each(function(i, v) {
		var formTag = document.createElement('form');
		
		var form_data = jQuery(this).html();
		var form_vars = /(:?\w+\s|)([\w\-]+?)\[(.+?)\]\;/g;
		
		var form_vmat; while(form_vmat = form_vars.exec(form_data)) {
			var form_items = /(\w+?)\s|(\w+?)\[(.+?)\]|([\w\-]+?)\[(.+?)\]/g;
			var form_field = undefined;
			var form_ir; while(form_ir = form_items.exec(form_vmat[0])) {
				if(form_ir[1]) {
					if(!form_field) form_field = document.createElement(form_ir[1]);
				} else if(form_ir[2] && form_ir[3]) {
					if(form_field) {
						jQuery(form_field).attr(form_ir[2], form_ir[3]);
					}
				} else if(form_ir[4] && form_ir[5]) {
					if(form_ir[4] == "form-method") formTag.method = form_ir[5];
					else if(form_ir[4] == "form-action") formTag.action = form_ir[5];
				}
			}
			if(form_field) jQuery(formTag).append(form_field);
		}
		
		var html_data = /\[html\-data\:\:start\]([\w\W]*?)\[html\-data\:\:end\]/g;
		var html_dtar = html_data.exec(form_data);
		if(html_dtar) {
			var html_sdtp = String(html_dtar[1]).replace(new RegExp("’|‘|′", "g"), "'");
			jQuery(formTag).append(html_sdtp.replace(/\[(\w+?)\s([\w\W]+?)\]/g, '<$1 $2 />'));
		}
		
		var script_data = /\[javascript\:\:start\]([\w\W]*?)\[javascript\:\:end\]/g;
		var script_dtar = script_data.exec(form_data);
		if(script_dtar) {
			jQuery.scriptToEval = script_dtar[1].replace(/\u2018|\u2019/g, "'");
			jQuery.scriptToEval = jQuery.scriptToEval.replace(/\u201C|\u201D/g, "''");
			jQuery.scriptToEval = jQuery.scriptToEval.replace(/\u2026/g, "...");
		}
		
		jQuery(this).after(formTag);
		jQuery(this).remove();
	});
	if(jQuery.scriptToEval != "") eval(jQuery.scriptToEval);
}

jQuery.isYoutubeVideo = "no";

jQuery(document).ready(function() {

	aWeberContlr();

	jQuery('body').wrapInner('<div class="body_wrapper" />');
	//jQuery('body').append("<div id='img_loader' style='width:"+jQuery(document).width()+";height:"+jQuery(document).height()+"; background:url(/squeezepage/loader.gif) no-repeat -50%; -50%S'>&nbsp;</div>");
	jQuery('body').css({'display' : 'block'});

	jQuery.videoPath = jQuery('.videopath').html();
	if(jQuery.videoPath != '') {
		if(isUrl(jQuery.videoPath)) {
			var youtubeRegex = new RegExp("^http:\/\/(?:www\.)?youtube.com\/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)", "g");
			var youtubeMatch = youtubeRegex.exec(jQuery.videoPath);
			if(youtubeMatch) {
				jQuery.videoPath = youtubeMatch[1];
				jQuery.isYoutubeVideo = "yes";
			}
		} else {
			jQuery.isYoutubeVideo = "yes";
		}
		jQuery('.videopath').remove();
	}

	var classids = String(jQuery('.script_classid').html());
	if(classids != '') {
		classids = classids.replace(new RegExp("^\s*|\"", "g"), "").replace(new RegExp("\"|\s*$", "g"), "");
		classids_arr = classids.split('|');
		for(var i = 0; i < classids_arr.length; i++) {
			jQuery('.' + classids_arr[i]).attr({'id' : classids_arr[i]});
			jQuery('.' + classids_arr[i]).removeClass(classids_arr[i]);
		}
	}
	jQuery('.script_classid').remove();

	total_loaded_script = jQuery('.javascript_load').length;
	current_loaded_script = 0;
	
	exec_scripts = function() {
		jQuery('.javascript').each(function(i, v) {
			html = jQuery(this).html();
			if(html != '') {
				var newScript = document.createElement('script');
				newScript.type = "text/javascript";
				newScript.text = "try { " + html.replace(new RegExp("’|‘|′", "g"), "'") + " } catch(e) { }";
				if(jQuery.browser.msie) {
					jQuery(newScript).appendTo(jQuery(this).parent());
				} else {
					jQuery(jQuery(this).parent()).get(0).appendChild(newScript);
				}
			}
			jQuery(this).remove();
		});
		
		jQuery('.style_head_src').each(function(i, v) {
			jQuery('head').append("<link rel='stylesheet' href='" + jQuery(this).html() + "' />");
			jQuery(this).remove();
		});
		
		var styles_text = "";
		jQuery('.style_head_append').each(function(i, v) {
			var style_source = String(jQuery(this).html()).replace(new RegExp("’|‘|′", "g"), "'");
			style_source = style_source.replace(/\;\s+?\./g, ";} .");
			style_source = style_source.replace(/\;\s+?\#/g, ";} #");
			jQuery('head').append('<style type="text/css">' + style_source + '</style>');
			//jQuery('head').append('<style type="text/css">' + String(jQuery(this).html()).replace(new RegExp("’|‘|′", "g"), "'") + '</style>');
			styles_text += String(jQuery(this).html()).replace(new RegExp("’|‘|′", "g"), "'");
			jQuery(this).remove();
		});	
		
		//var rgPatt = new RegExp("(.+?){(.+?)}", "gi");
		//while (rgMatch = rgPatt.exec( styles_text )) {
		//	alert(rgMatch[1] + ' == ' + rgMatch[2]);
		//}
		
		/*var rgMatch = styles_text.match(/(.+?){(.+?)}/gi);
		if(rgMatch) {
			for(var i = 0; i < rgMatch.length; i++) {
				alert(rgMatch[i]);
			}
		}*/
		
		//jQuery("body").css({'background' : 'url("");'});

		//var body_html = jQuery('.body_wrapper').html();
		//jQuery('body').html(body_html);
		//jQuery('body').unwrap();
		
		//var body_html = jQuery('.body_wrapper').html();
		//jQuery('.body_wrapper').parent().html(jQuery('.body_wrapper').html());
		
		//jQuery('body').css({'display' : 'none'});
		jQuery('#img_loader').remove();
		setTimeout("jQuery('.body_wrapper').fadeIn();", 500);
	};
	
	load_counter = function() {
		if(current_loaded_script == (total_loaded_script - 1)) {
			//alert('Calling script initalizer');
			exec_scripts();
		} else {
			current_loaded_script++;
			//alert('Counter: ' + current_loaded_script + '/' + (total_loaded_script - 1));
		}
	}
	
	if(total_loaded_script == 0) {
		exec_scripts();
	} else {
		var script_counter = 1;
		var tmp_object = [];
		jQuery('.javascript_load').each(function(i, v) {
			source = jQuery(this).html();
			
			var newScript = document.createElement('script');
			newScript.type = "text/javascript";
			newScript.id = ('loaded_script_' + script_counter);
			newScript.src = source;
			
			newScript.onload = newScript.onreadystatechange = function() {
				if(!this.readyState || this.readyState == 'loaded' || this.readyState == "complete") {
					load_counter();
					//alert(this.src);
					this_target = jQuery("#"+this.id).get(0);
					this_target.onload = this_target.onreadystatechange = null;
					//newScript.onload = newScript.onreadystatechange = null;
				}
			}
			
			newScript.onerror = function() {
				load_counter();
				this_target = jQuery("#"+this.id).get(0);
				this_target.onload = this_target.onreadystatechange = null;
				//newScript.onload = newScript.onreadystatechange = null;
			}
			
			tmp_object[i] = {};
			tmp_object[i]['target'] = this;
			tmp_object[i]['object'] = newScript;
			
			/*if(jQuery.browser.msie && (jQuery.browser.version != 8.0)) {
				jQuery(newScript).appendTo(jQuery(this).parent());
			} else {
				jQuery(jQuery(this).parent()).get(0).appendChild(newScript);
			}*/
			
			//jQuery(this).remove();
			script_counter++;
		});	
		
		var timeout_counter = 0;
		do_insert = function() {
				if(jQuery.browser.msie && (jQuery.browser.version != 8.0)) {
					jQuery(tmp_object[timeout_counter]['object']).appendTo(jQuery(tmp_object[timeout_counter]['target']).parent());
				} else {
					jQuery(jQuery(tmp_object[timeout_counter]['target']).parent()).get(0).appendChild(tmp_object[timeout_counter]['object']);
				}
				jQuery(tmp_object[timeout_counter]['target']).remove();
				timeout_counter++;
				if(timeout_counter < tmp_object.length) setTimeout( "do_insert()", 50 );
		}; do_insert();
	}
});


