$j = jQuery.noConflict();

$j(document).ready(function() {

	var isTouch = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|android)/);
	
	//InitFonts();
	
	CustomizeMenu();
	
	RollUpMenu();
	
	ApplyColorbox();
	
	ApplyFancyboxVideo();
	
	ProductsMove();
	
	InitMisc();
	
	HoverZoomInit();
	
	OpenCloseShortcode();
	
});

function InitFonts(){
	// Font replacement
	// titles font
	//Cufon.replace('h1, h2, h3, h4');
	// menus font
	//Cufon.replace('.mainmenu > ul.menu > li > a', {hover: {color: '#6C7E00'}});
	
	// Cufon.replace('.page-content .widget_posts h3 a', {hover: {color: '#333333'}});
	// Cufon.replace('.hentry .comments-box .comments-link a, .hentry h2.entry-title a, .sc-page h3 a', {hover: {color: '#333333'}});
	// Cufon.replace('.services h2 a', {hover: {color: '#333333'}});
	// Cufon.replace('.services a.more', {hover: {color: '#333333'}});
	
	// Cufon.replace('.hentry .date-box .date, .hentry .date-box .month'); 
	
	// Cufon.replace('a.bigbut, .sc-button .title', {textShadow: '-1px -1px rgba(0, 0, 0, 0.1)'});
}

function CustomizeMenu(){
	$j(".mainmenu > ul > li").each(function(){
		if($j(this).has('ul').length){
			$j(this).addClass("parent");	
		}
	});
}

function RollUpMenu(){
	var time = parseInt($j('#mainmenu-dropdown-duration').text());
	var easing = $j('#mainmenu-dropdown-easing').text();
	
	$j(".mainmenu ul > li").hover(function(){
		var submenu = $j(this).find(' > ul');
		var submenuHeight = parseInt(submenu.height());

		submenu.css("display","block");
		submenu.height("1px");

		$j(this).find(' > ul').stop().animate({
			height: submenuHeight
		}, time, easing);
	}, function(){
		$j(this).find(' > ul').css("display","none");
	});

}

function ApplyColorbox(){
	// Apply fancybox on all images
	$j("a[href$='gif']").colorbox({rel: 'group', maxHeight:"95%"});
	$j("a[href$='jpg']").colorbox({rel: 'group', maxHeight:"95%"});
	$j("a[href$='png']").colorbox({rel: 'group', maxHeight:"95%"});
}
function ApplyFancyboxVideo(){
	// AIT-Portfolio videos
	$j(".ait-portfolio a.video-type").click(function() {

		var address = this.href
		if(address.indexOf("youtube") != -1){
			// Youtube Video
			$j.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		} else if (address.indexOf("vimeo") != -1){
			// Vimeo Video
			// parse vimeo ID
			var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
			var match = this.href.match(regExp);

			if (match){
			    $j.fancybox({
					'padding'		: 0,
					'autoScale'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'title'			: this.title,
					'width'			: 680,
					'height'		: 495,
					'href'			: "http://player.vimeo.com/video/"+match[2]+"?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff",
					'type'			: 'iframe'
				});
			} else {
			    alert("not a vimeo url");
			}
		}
		return false;
	});

	// Images shortcode
	$j("a.sc-image-link.video-type").click(function() {

		var address = this.href
		if(address.indexOf("youtube") != -1){
			// Youtube Video
			$j.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'elastic',
				'transitionOut'	: 'elastic',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
					'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});
		} else if (address.indexOf("vimeo") != -1){
			// Vimeo Video
			// parse vimeo ID
			var regExp = /http:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/;
			var match = this.href.match(regExp);

			if (match){
			    $j.fancybox({
					'padding'		: 0,
					'autoScale'		: false,
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'title'			: this.title,
					'width'			: 680,
					'height'		: 495,
					'href'			: "http://player.vimeo.com/video/"+match[2]+"?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff",
					'type'			: 'iframe'
				});
			} else {
			    alert("not a vimeo url");
			}
		}
		return false;
	});
}

function ProductsMove(){
	
	$j('ul#products-list').jcarousel({
		easing : 'easeInOutBack'
	});
	
	/*
	var productsCount = 0;
	var actualPosition = 0;
	
	var productsPosition = $j(".product-container ul").position();
	var productsWidth = $j(".product-container ul").width();
	
	$j(".product-container ul li").each(function(index) {
		productsCount++;
	});
	
	if(productsCount<=6){
		$j('.product-nav .next').addClass('off');
	}
	
	
	$j('.product-nav .next').click(function() {
		if (actualPosition <= productsCount-6) {
			$j(".product-container ul").animate({"left": "-=850px"}, 500, 'easeInOutBack');
			actualPosition++;
			SetArrows(actualPosition,productsCount);
		}
	});
	
	$j('.product-nav .prev').click(function() {
		if (actualPosition > 0) {
			$j(".product-container ul").animate({"left": "+=850px"}, 500, 'easeInOutBack');
			actualPosition--;
			SetArrows(actualPosition,productsCount);
		}
	});
	*/
}
function SetArrows(actualPosition,productsCount){
	if(actualPosition > 0){
		$j('.product-nav .prev').removeClass('off');
	} else {
		$j('.product-nav .prev').addClass('off');
	}
	if(actualPosition <= productsCount-6){
		$j('.product-nav .next').removeClass('off');
	} else {
		$j('.product-nav .next').addClass('off');
	}
}
function InitMisc() {
  $j('#content input, #content textarea').each(function(index) {
    var id = $j(this).attr('id');
    var name = $j(this).attr('name');
    if (id.length == 0 && name.length != 0) {
      $j(this).attr('id', name);
    }
  });

  $j('#content label').inFieldLabels();

  $j('.rule span').click(function() {
	  $j.scrollTo(0, 1000, {axis:'y'});
	  return false;
 });

}
function HoverZoomInit() {
  //// Post images
  //$j('#content .entry-thumbnail a').hoverZoom({overlayColor:'#333333',overlayOpacity: 0.8});

  // default wordpress gallery
  $j('#content .gallery-item a').hoverZoom({overlayColor:'#333333',overlayOpacity: 0.8,zoom:0});

  // ait-portfolio
  $j('#content .ait-portfolio a').hoverZoom({overlayColor:'#333333',overlayOpacity: 0.8,zoom:0});

  // schortcodes
  $j('#content a.sc-image-link').hoverZoom({overlayColor:'#333333',overlayOpacity: 0.8,zoom:0});

}

function OpenCloseShortcode(){
	
	//$j('#content .frame .frame-close.closed').parent().find('.frame-wrap').hide();
	$j('#content .frame .frame-close.closed .close.text').hide();
	$j('#content .frame .frame-close.closed .open.text').show();
	
	$j('#content .frame .frame-close').click(function(){
		if($j(this).hasClass('closed')){
			var $butt = $j(this);
			$j(this).parent().find('.frame-wrap').slideDown('slow',function(){
				$butt.removeClass('closed');
				$butt.find('.close.text').show();
				$butt.find('.open.text').hide();
			});
		} else {
			var $butt = $j(this);
			$j(this).parent().find('.frame-wrap').slideUp('slow',function(){
				$butt.addClass('closed');
				$butt.find('.close.text').hide();
				$butt.find('.open.text').show();
			});
			
		}
		
	});
}

