var timer;
var hideDelay = 500

window.addEvent('domready', function() {
	if ($E('.moduletable_nppod')) {
		var h3 = $E('.moduletable_nppod').getElement('h3');
		
		//span tag
		var span = $(h3).getChildren()[0];
		$(span).set('html', $(span).get('html').replace("||", "<br>"));

	}
	if ($E('.moduletable_jatabs')) {
		var h3 = $E('.moduletable_jatabs').getElement('h3');
		
		//span tag
		var span = $(h3).getChildren()[0];
		$(span).set('html', $(span).get('html').replace("||", "<br>"));
	}
	
	if ($E('.moduletable_findshop')) {
		var h3 = $E('.moduletable_findshop').getElement('h3');
		
		//span tag
		var span = $(h3).getChildren()[0];
		$(span).set('html', $(span).get('html').replace("||", "<br>"));
	}
	
	
	// use in newsletter
	if ($E('.newsletter-subscription-block')) {
		$('submit_btn').addEvent('click', function() {
			if ($('newsletter_email')) {
				var val = $('newsletter_email').get('value');
				var url = $('redirect_url').get('value');
				if (url.indexOf("?") != -1) {
					url = url + "&email=" + escape(val);
				} else {
					url = url + "?email=" + escape(val);
				}
				document.location = url;
			}
		});
	}
	
	if ($('newsletter_form')) {
		$('email').getParent().adopt( new Element('div', {'id':'er-email', 'class':'error-message'}) );
		$('er-email').setStyles({'color':'#ff0000', 'font-size':10});
		
		var pair = getPairFromUrl();
		if ( pair["email"] ) {
			$('email').set('value', pair["email"]);
		}
		
		$('newsletter_form').addEvent('submit', function() {
			var isSubmit = true;
			$('er-email').innerHTML = "";
		
			if ( $('email').value == "" ) {
				$('er-email').innerHTML = "Please enter the email address";
				isSubmit = false;
			}
			if ( $('email').value != "" ) {
				if (!isValidEmailFormat( $('email').value ) ) {
					$('er-email').innerHTML = "Please enter a valid email address";
					isSubmit = false;
				}
			}
			return isSubmit;
		});
	}
	/*
	var isAddedHeight = false;
	var li = $('ja-megamenu').getChildren().getChildren();
	Array.each(li, function(v, i) {
		for (var j=0; j<v.length; j++) {
			if (j == 1) {
				var div = v[j].getChildren()[1];
				v[j].addEvent('mouseenter', function(e) {
					if (!isAddedHeight) {
						var h = $(div).getSize().y + 20
						$(div).setStyles({'margin-left':'0px', 'left':'0px', 'background-color':'#E6E6E6', 'height':h + 'px'});	
						if (document.all) {
							fixMarginLeft(div);
						}
					} else {
						$(div).setStyles({'margin-left':'0px', 'left':'0px', 'background-color':'#E6E6E6'});	
					}
					isAddedHeight = true;
					e.stop();
				});
				v[j].addEvent('mouseleave', function(e) {
					$(div).setStyles({'left':'-999em'});
					e.stop();
				});
				$(div).setStyles({'width':'980px'});
			}
			
		}
	});
	*/
//	alert($('ja-quickaccess'));
	if ($('ja-quickaccess')) {
		var timerId = null;
		var li = $('ja-quickaccess').getChildren()[0].getChildren()[0];
		var a = $(li).getChildren()[0];
		
		var ul = $(li).getChildren()[1];
		highlightSubItem(ul);
			
		$('ja-quickaccess').addEvent('mouseover', function(event) {
			a.addClass('over');
			clearTimeout(timerId);
			event.stop();
			$(ul).setStyle('display', 'block');
			return false;
		});
		$('ja-quickaccess').addEvent('mouseout', function(event) {
			var run = function() {
				a.removeClass('over');
				$(ul).setStyle('display', 'none');
			}
			timerId = setTimeout(run, 250);
			event.stop();
			return false;
		});
	}
	
	if ($E('body.com_user')) {
		var topPanel = $E('body.com_user').getElementById("ja-top-panel");
		var h3 = topPanel.getElement('h3');
		var span = h3.getElement('span');
		var aElement = new Element("a").set('href', '/index.php?option=com_virtuemart&Itemid=305');
		aElement.inject(h3);
		span.inject(aElement);
		
		var div = $E('body.com_user').getElementById("ja-right");
		div.addEvent('mouseover', function() {
			showDropDown();	
		});
		div.addEvent('mouseout', function() {
			hideDropDown();	
		});
		
		aElement.addEvent('mouseover', function() {
			showDropDown();	
		});
		aElement.addEvent('mouseout', function() {
			hideDropDown();	
		});
	}
	// main page news module add link
	if ($('Mod131')) {
		var head = $('Mod131').getChildren()[0].getChildren()[0];
		var href = '/news/blog.html';
		var aElement =  new Element('a', {'id':'', 'class':'new-link', 'href':href});
	//	aElement.innerHTML = head.innerHTML;
		aElement.wraps(head);
//		$(head).getParent().adopt( aElement );
	}
});

function isValidEmailFormat(str) {
		
//	var re = /^[a-zA-Z0-9_-]+\@{1}[a-zA-Z0-9_-]+\.\S+$/;	
	var re = /^\S*\@\S*\.\S*$/;
	return re.test(str);
}

function highlightSubItem(ul) {
	$(ul).getChildren().addEvent('mouseover', function() {
		$(this).setStyle('background-color', '#484848');
		//$(this).addClass("over");
	})
	
	$(ul).getChildren().addEvent('mouseout', function() {
		$(this).setStyle('background-color', 'none');
		//$(this).removeClass("over");
	})
	/*
	Array.each(ul.getChildren(), function(v, i) {
		$(v).addEvent('mouseover', function() {
			$(v).addClass("over");
		})
		
		$(v).addEvent('mouseout', function() {
			$(v).removeClass("over");
		})
	})
	*/
}

function fixMarginLeft(div) {
	var run = function() {
		$(div).setStyles({'margin-left':'0px'});
	}
	setTimeout(run, 200);
}

function getPairFromUrl() {
	var s = document.location.search;
	s = s.replace("?", "");
	var sArray = s.split("&");
	var sParams = {};
	
	for (var i=0; i<sArray.length; i++) {
		var items = sArray[i].split("=");
		sParams[items[0]] = items[1];
	}
	return sParams;
}

function isNewsletterFormValid() {
	
	var email = $('email').get('value');
	var country = $('Country_').get('value');
	if (!(/^\S*\@\S*\.\S*$/.test(email))) {
		alert('Email not valid, please enter again!');	
		return false;
	}
	
	if (country == "") {
		alert("Please select the country");
		return false;
	}
	return true;
} 

function showDropDown() {
	clearTimeout(timer);
	var div = $E('body.com_user').getElementById("ja-right");
	div.setStyles({
		'display': 'block'
	});
}

function hideDropDown() {
	var run = function() {
		var div = $E('body.com_user').getElementById("ja-right");
		div.setStyles({
			'display': 'none'
		});
	}
	timer = setTimeout(run, hideDelay);
}


var containerBorderSize = 10;
	var swfWidth = 640
	var swfHeight = 360
	var url = "";
	
	function showVideoPlayer(paramURL) {
		url = paramURL;
		showOverlay();
	}

	function showOverlay() {
		var divstr = '';
		divstr = '<div id="jquery-overlay"></div>';
		divstr += '<div id="jquery-lightbox-container">';
		divstr += '	<div id="jquery-lightbox">';
		divstr += '		<div id="lightbox-container-image-box">';
		divstr += '			<div id="lightbox-close"><a href="javascript:;"></a></div>';
		divstr += '			<div id="lightbox-container-image"></div>';
		divstr += '		</div>';
		divstr += '	</div>';
		divstr += '</div>';
		
		
		
		jQuery('body').append(divstr);	
		// Get page sizes
		var arrPageSizes = ___getPageSize();
		// Style overlay and show it
		jQuery('#jquery-overlay').css({
			backgroundColor:	'#000',
			opacity:			0.8,
			width:				arrPageSizes[0],
			height:				arrPageSizes[1]
		}).fadeIn();
		_resize_container_image_box(swfWidth, swfHeight);

		jQuery('#jquery-overlay').click(function() {
			_finish();									
		});
		// Assign the _finish function to lightbox-loading-link and lightbox-secNav-btnClose objects
		jQuery('#lightbox-loading-link,#lightbox-close').click(function() {
			_finish();
			return false;
		});

		jQuery(window).resize(function() {
			// Get page sizes
			var arrPageSizes = ___getPageSize();
			// Style overlay and show it
			jQuery('#jquery-overlay').css({
				width:		arrPageSizes[0],
				height:		arrPageSizes[1]
			});
			// Get page scroll
			var arrPageScroll = ___getPageScroll();
//			var tmpTop = arrPageScroll[1] + (arrPageSizes[3] / 2) - (swfHeight/2);
//			var tmpLeft = arrPageScroll[0] + (arrPageSizes[2] / 2) - (swfWidth/2);
			var tmpTop = (arrPageSizes[3] / 2) - (swfHeight/2);
			var tmpLeft = (arrPageSizes[2] / 2) - (swfWidth/2);
			jQuery('#jquery-lightbox-container').css({
				top:	tmpTop,
				left:	tmpLeft
			});
		});

		jQuery(window).trigger('resize');
	}

	function _resize_container_image_box(intImageWidth,intImageHeight) {
		var intCurrentWidth = jQuery('#lightbox-container-image-box').width();
		var intCurrentHeight = jQuery('#lightbox-container-image-box').height();
		// Get the width and height of the selected image plus the padding
		var intWidth = (intImageWidth + (containerBorderSize * 2)); // Plus the image愀 width and the left and right padding value
		var intHeight = (intImageHeight + (containerBorderSize * 2)); // Plus the image愀 height and the left and right padding value
		// Diferences
		var intDiffW = intCurrentWidth - intWidth;
		var intDiffH = intCurrentHeight - intHeight;
		// Perfomance the effect
		jQuery('#lightbox-container-image-box').animate({ width: intWidth, height: intHeight },400,function() { 
			_set_content();
		});
	};

	function _finish() {
		jQuery('#jquery-lightbox-container').remove();
		jQuery('#jquery-overlay').fadeOut(function() { jQuery('#jquery-overlay').remove(); });
		// Show some elements to avoid conflict with overlay in IE. These elements appear above the overlay.
		jQuery('embed, object, select').css({ 'visibility' : 'visible' });
	}

	function ___getPageSize() {
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	}

	function ___getPageScroll() {
		var xScroll, yScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;	
		}
		arrayPageScroll = new Array(xScroll,yScroll);
		return arrayPageScroll;
	}
	
	function _is_local_video() {
		var aryExt = [".flv", ".f4v"];
		var isVideo = false;
		for (var i=0; i<aryExt.length; i++) {
			if (url.lastIndexOf(aryExt[i]) != -1) {
				isVideo = true
			}
		}
		return isVideo;
	}

	function _set_content() {
		var swfcontent = '';
		jQuery('#lightbox-close').show();
		if (!_is_local_video()) {
			var youtubeURL = "http://www.youtube.com/v/" + url + "&amp;hl=en_US&amp;fs=1&amp;hd=1&amp;autohide=1";
			swfcontent = '<object width="' + swfWidth + '" height="' + swfHeight + '"><param name="movie" value="' + youtubeURL + '"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="opaque"></param><param name="allowscriptaccess" value="always"></param><embed src="' + youtubeURL + '" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="opaque" allowfullscreen="true" width="' + swfWidth + '" height="' + swfHeight + '"></embed></object>';
		} else {
			swfcontent = '<object width="' + swfWidth + '" height="' + swfHeight + '"><param name="movie" value="/media/PrydeGroup/Productview/swf/jwplayer/player.swf"></param><param name="allowFullScreen" value="true"></param><param name="wmode" value="opaque"></param><param name="flashvars" value="file='+url+'&autostart=true"></param><param name="allowscriptaccess" value="always"></param><embed src="/media/PrydeGroup/Productview/swf/jwplayer/player.swf" flashvars="file='+url+'&autostart=true" type="application/x-shockwave-flash" allowscriptaccess="always" wmode="opaque" allowfullscreen="true" width="' + swfWidth + '" height="' + swfHeight + '"></embed></object>';
		}
		jQuery('#lightbox-container-image').html(swfcontent);
	}

