$j = jQuery.noConflict();

$j(document).ready(function() {

	var isTouch = navigator.userAgent.toLowerCase().match(/(iphone|ipod|ipad|android)/);
	
	//InitFonts();
	
	CustomizeMenu();
	
	RollUpMenu();
	
	ApplyColorbox();
	
	ApplyFancyboxVideo();
	
	//IconMenuSizeing() ;
	
	//SBoxSizeing();
	
	widgetsSize();
	
	ProductsMove();
	
	InitMisc();
	
	HoverZoomInit();
	
	OpenCloseShortcode();
	
	PrettySociableInit();
	
	notificationClose();
	
	//sliderBuyNowClick();
	
	//refresOnResize();
});



function refresOnResize() {
		$j(window).bind('resize',function(){
		 window.location.href = window.location.href;
	});
}

function sliderBuyNowClick()
{
  $j("ul#slider").children("li").each(function(){
    var link = $j(this).find(".buyNow").attr("rel");
    $j(this).find(".buyNow").click(function(){
      window.location = link;
    });
  });
}

function PrettySociableInit(){
	
	var homeUrl = $j("body").data("themeurl");
	
	$j.prettySociable({websites: {
				facebook : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'Facebook',
					'url': 'http://www.facebook.com/share.php?u=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/facebook.png',
					'sizes':{'width':70,'height':70}
				},
				twitter : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'Twitter',
					'url': 'http://twitter.com/home?status=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/twitter.png',
					'sizes':{'width':70,'height':70}
				},
				delicious : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'Delicious',
					'url': 'http://del.icio.us/post?url=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/delicious.png',
					'sizes':{'width':70,'height':70}
				},
				digg : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'Digg',
					'url': 'http://digg.com/submit?phase=2&url=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/digg.png',
					'sizes':{'width':70,'height':70}
				},
				linkedin : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'LinkedIn',
					'url': 'http://www.linkedin.com/shareArticle?mini=true&ro=true&url=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/linkedin.png',
					'sizes':{'width':70,'height':70}
				},
				reddit : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'Reddit',
					'url': 'http://reddit.com/submit?url=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/reddit.png',
					'sizes':{'width':70,'height':70}
				},
				stumbleupon : {
					'active': true,
					'encode':false, // If sharing is not working, try to turn to false
					'title': 'StumbleUpon',
					'url': 'http://stumbleupon.com/submit?url=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/stumbleupon.png',
					'sizes':{'width':70,'height':70}
				},
				tumblr : {
					'active': true,
					'encode':true, // If sharing is not working, try to turn to false
					'title': 'tumblr',
					'url': 'http://www.tumblr.com/share?v=3&u=',
					'icon':homeUrl+'/design/img/prettySociable/large_icons/tumblr.png',
					'sizes':{'width':70,'height':70}
				}
			}});
}

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).children('ul');
		var size = $j(this).children('ul').children('li').size();
		
		$j(this).children('ul').children('li').each( function(){
			var sub = $j('.sub-menu').children('li');
			//alert(sub);
			liHeight = parseInt(sub.height());
			marT = $j('.sub-menu').children('li').css('marginTop').replace("px", "");
			marB = $j('.sub-menu').children('li').css('marginBottom').replace("px", "");
			//alert(marT);
		});
		
		var submenuHeight = ((liHeight*size)+(marT*size)+(marB*size));
		//alert(submenuHeight);
		
		//var submenuHeight = parseInt(submenu.height());

		submenu.css("display","block");
		submenu.height("1px");

		$j(this).children('ul').stop('true','true').animate({
			height: submenuHeight
		}, time, easing);
	}, function(){
		$j(this).children('ul').css("display","none");
		$j(this).children('ul').css('height','1px');
	});

}

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%"});
	
	$j(".csss").css('display','block');
}
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 (typeof id != "undefined" && typeof name != "undefined"){
		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();
			});
			
		}
		
	});
}

function IconMenuSizeing() {
	sizeMI = $j('ul#sti-menu > li').size();
	sizeUL = $j('ul.sti-menu').width();
	ulSize = parseFloat(sizeUL, 10);
	
	//alert((ulSize/sizeMI) -8);
	
	widthLI = ( ulSize/sizeMI );
	$j('ul#sti-menu > li').css('width',(widthLI-1));
	
	$j('ul#sti-menu > li').each( function() {
		widthH2 = $j('ul#sti-menu > li > a > h2').width() + 20;
		wH2 = (widthLI - widthH2)/2;
		$j('ul#sti-menu > li > a > h2').css('left',wH2);
		
		widthH3 = $j('ul#sti-menu > li > a > h3').width() +20;
		wH3 = (widthLI - widthH3)/2;
		$j('ul#sti-menu > li > a > h3').css('left',wH3);
		
		widthSpan = $j('ul#sti-menu > li > a > span').width();
		wSpan = (widthLI - widthSpan)/2;
		$j('ul#sti-menu > li > a > span').css('left',wSpan);
	});
	
	//$j('#sti-menu').css('display','block')
}

function SBoxSizeing() {
	sizeSBox = $j('div.sbox').size();	
	paddSbox = $j('div.services').css('paddingLeft');
	marSbox = $j('div.sbox').css('marginLeft');
	
	paddS = parseFloat(paddSbox, 10);
	marS = parseFloat(marSbox, 10);

	widthSbox = (1022 - ( paddS*2 ) - ((marS*sizeSBox)*2))/sizeSBox;
	$j('div.sbox').css('width', widthSbox);
}

function widgetsSize() {
	i=0;
	$j('div.widgets-inner > div.box').each( function() {
		$j('div.widgets-inner > div.box').eq(i).addClass('col-' + (i+1));
	i++;
	});
}

function notificationClose() {
	$j('.sc-notification').children('a.close').click( function(event) {
		event.preventDefault();
		$j(this).parent().fadeOut('slow');
	});
}




