(function($) {
	$.fn.initCycle = function(options) {
			if (typeof (options) == 'undefined') {
				options = {};
				$fx = $.fn.initCycle.defaults.fx
			}	else 
				$fx =  options.fx;
				
			$carousel	= $(this).find('.carousel');
			$nav		= $(this).find('.nav');
			$next		= $(this).find('.next');
			$previous	= $(this).find('.previous');
			$pager		= $(this).find('.nav-arrows');

			if($carousel.children().length > 1) {
				$pager.show();
				$nav.show();
				$carousel.cycle({
					fx: 	$fx,
					speed:	300,
					timeout: 0,
					pager: $nav,
					next:  $next,
					prev:  $previous
				});
			} 
	};

	$.fn.initCycle.defaults = {
		fx: 'scrollHorz'

	};
	var largeWidth = 500;
	var largeHeight = 500;
	var mediumWidth = 280;
	var mediumHeight = 280;

	$.fn.PanZoom = function(options) {
				
			if (typeof (options) == 'undefined') {
				options = {};
			}	else {
				largeWidth = options.largeWidth;
				largeHeight = options.largeHeight;
				mediumWidth = options.mediumWidth;
				mediumHeight = options.mediumHeight;
			}

			$j('.zoom').css({
				'width': mediumWidth + 'px',
				'height': mediumHeight + 'px'
			});
			$j('.large').hide();
			$j('.zoom').mouseover(function() {
				$j('.medium').hide();
				$j('.large').show();
			});
			$j('.zoom').mouseout(function() {
				$j('.medium').show();
				$j('.large').hide();
			});
			$j('.zoom').mousemove(function(e) {
				var mouseX = (e.pageX-215) - $j(this).attr('offsetLeft');
				var mouseY = (e.pageY-215) - $j(this).attr('offsetTop');

				var posX = (Math.round((mouseX / mediumWidth) * 100) / 100) * (largeWidth - mediumWidth);
				var posY = (Math.round((mouseY / mediumHeight) * 100) / 100) * (largeHeight - mediumHeight);

				$j('.large').css({
					'left': '-' + posX + 'px',
					'top': '-' + posY + 'px'
				});
			});
	};
})(jQuery);

$j(document).ready(function() {

	$j('.acc-element > span.button').show();
	$j('.acc-element.selected > .modBodySidebar').show();
	$j(function() {
		// setup ul.tabs to work as tabs for each div directly under div.panes
		$j("ul.tabs").tabs("div.tab-container > div.tab-content", {effect: 'fade'});
	});
	$j(function() {
		// setup ul.tabs to work as tabs for each div directly under div.panes
		$j("ul.phone_detail_tab").tabs("div.phone_detail_tab_container > div.tab-content", {
			effect: 'fade'
		});
	});
	
});

function loadPopup(theURL, theID){
	$j.ajax({
		cache: false,
		url:  theURL,
		dataType: "html",
		success: function(data) {
			$j.fancybox({
				'hideOnContentClick': false,
				'padding'		:	0,
				'margin'		:	20,
				'width'			:	575,
				'height'		:	700,
				'hideOnOverlayClick':true,
				'autoScale'		:	false,
				'overlayColor'	:	'#000',
				'autoDimensions':	false,
				'overlayOpacity':	0.8,
				'titlePosition'	:	'over',
				'content'		:  $j('<div class=\"popupContainer\" id=\"popupContainer'+theID+'\">'+$j(data).filter('#MainContainer').html()+'</div>'),
				onComplete		:	function(){
					//alert($j('.popupContainer .scrollable').html());
					$j('#popupContainer'+theID+' .total_info').html(' / '+$j("#popupContainer"+theID+" .item_thumb").size());
					$j('#popupContainer'+theID+' .current_info').html('1');
					if ($j("#popupContainer"+theID+" .item_thumb").size() > 6){
						$j('#popupContainer'+theID+' .prev').removeClass('disabled')
						$j('#popupContainer'+theID+' .next').removeClass('disabled')
					}
					$j('#popupContainer'+theID+' .scrollable').scrollable({
						//circular : true
					});
				}

			});
		}
	});
	//return false;
}


var loadImage = function(url,ObjId,link,title){
	var wrap = $j("#simpleImageGallery"+ObjId+" .bigImageWraper .holder").fadeTo("medium", 0, function (){
	$j("#simpleImageGallery"+ObjId+" .item_title").text($j(link).attr("title"))
	var img = new Image();
	img.src = url;
	img.onload = function() {

			// make wrapper fully visible
			wrap.fadeTo("fast", 1);

			// change the image
			wrap.find("img").attr("src", url);

		};

	});
	//alert($j("#simpleImageGallery"+ObjId+" a"));
	$j("#simpleImageGallery"+ObjId+" a").removeClass("active");
	$j(link).addClass("active");
	$j("#simpleImageGallery"+ObjId+" .current_info").text($j(link).attr("rel"));
	return false
}


function loadNewPopup(theURL, theID){
		$j.ajax({
		cache: false,
		url:  theURL,
		dataType: "html",
		success: function(data) {
			$j.fancybox({
				'hideOnContentClick': false,
				'padding'		:	0,
				'margin'		:	20,
				'width'			:	670,
				'height'		:	600,
				'hideOnOverlayClick':true,
				'autoScale'		:	false,
				'overlayColor'	:	'#000',
				'autoDimensions':	false,
				'overlayOpacity':	0.8,
				'titlePosition'	:	'over',
				'content'		:  $j('<div class=\"popupContainer\" id=\"popupContainer'+theID+'\">'+$j(data).filter('#MainContainer').html()+'</div>'),
				onComplete		:	function(){
					var gallery = $j($j('#popupContainer'+theID+' #gallery_navigation')).galleriffic({
						numThumbs:					7,
						preloadAhead:              1,
						renderSSControls:          false,
						renderNavControls:         false,
						imageContainerSel:			$j('#popupContainer'+theID+' #image_holder'),
						enableBottomPager:			true,
						bottomPagerContainerSel:	$j('#popupContainer'+theID+' #bottom'),
						prevPageBottomPagerSel:		$j('#popupContainer'+theID+' #bottom_pager .prev'),
						nextPageBottomPagerSel:		$j('#popupContainer'+theID+' #bottom_pager .next'),
						controlsContainerSel:      $j('#popupContainer'+theID+' #controls'),
						captionContainerSel:		$j('#popupContainer'+theID+' #caption'),
						loadingContainerSel:       $j('#popupContainer'+theID+' #loading'),
						prevPageLinkText:			'',
						nextPageLinkText:			'',
						syncTransitions:           false,
						onSlideChange:             function(prevIndex, nextIndex) {
							$j('#popupContainer'+theID+' #photo-index').html('	Photo '+ (nextIndex+1) +' / '+ this.data.length);
							//alert ($j('#popupContainer'+theID+' #bottom').html());
							//$j.fancybox.resize();
						},
						onTransitionOutStart:		function(){
							var containerH = this.$imageContainer.height();
							var captionH = this.$captionContainer.height();
							var newLoadingH = containerH + captionH;
							//this.$loadingContainer.find('.animation').height(containerH);
							this.$loadingContainer.height(newLoadingH);
						},
						onTransitionIn:		function(newSlide, newCaption, isSync){
							$j.fancybox.resize();
							newSlide.fadeTo(this.getDefaultTransitionDuration(isSync), 1.0);
						},
						onSlideShowPause:			function(){
							//alert("haleluja");
							this.$controlsContainer.find('a.play').show();
							this.$controlsContainer.find('a.pause').hide();
						},
						onSlideShowPlay:			function(){
							//alert("Started");
							this.$controlsContainer.find('a.play').hide();
							this.$controlsContainer.find('a.pause').show();
						},
						onPageTransitionOut:       function(callback) {
							this.fadeTo('fast', 0.0, callback);
						},
						onPageTransitionIn:        function() {
							this.fadeTo('fast', 1.0);
						}
					});
					gallery.$controlsContainer.find('a.next').click(function(e) {
						gallery.next(false);
						e.preventDefault();
					});
					gallery.$controlsContainer.find('a.prev').click(function(e) {
						gallery.previous();
						e.preventDefault();
					});
					gallery.$controlsContainer.find('a.play').click(function(e) {
						gallery.play();
						$j(this).hide();
						gallery.$controlsContainer.find('a.pause').show();
						e.preventDefault();
					});
					gallery.$controlsContainer.find('a.pause').click(function(e) {
						gallery.pause();
						$j(this).hide();
						gallery.$controlsContainer.find('a.play').show();
						e.preventDefault();
					});
				}

			});
		}
	});
	//return false;
}
