//// Font Size
var min=8;
var max=18;
function increaseFontSize() {
   var p = document.getElementsByTagName('body');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseFontSize() {
   var p = document.getElementsByTagName('body');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}

var MooSwap = new Class({   
    //implements
    Implements: [Options],
    options: {
        imgHoverPrefix: '_over'
    },    
    initialize: function(elements, options) {
        //set options
        this.setOptions(options);
        // Set elements
        this.setSwap(elements);
    },    
    setSwap: function(elements) {          
        var prefix = this.options.imgHoverPrefix;        
        // preload images array
        imgTemp = [];
        i = 0;        
        $$(elements).each(function(el) {
            var mouseFx = new Fx.Tween(el, {duration: 240, wait: false});
            var holdSrc = el.getProperty('src');   
            var extension = holdSrc.substring(holdSrc.lastIndexOf('.'),holdSrc.length);       
            var newSrc = holdSrc.replace(extension, prefix + '' + extension);            
            // set new image for preloading
            imgTemp[i] = new Asset.image(newSrc, { alt: el.getProperty('alt'), id: el.getProperty('id') });      
            // default link on current img element
            var link = el;           
            // check if there is a link a href parent
            var test = el.getParent('a');
            if (test) {
                var link = test.setProperty('id', '__link_' + i);        
            }            
            link.addEvents({
                mouseover: function() {
                    el.setProperty('src', newSrc);
                },
                mouseout: function() {
                    el.setProperty('src', holdSrc);
                }
           });           
           i++;
        });    
    }
});


window.addEvent('domready', function() {

////////////////////////////////Menu Show/Hide

var theMenu = $('menu-slide');

var tweenMenu = function(theWidth){
	theMenu.set('tween', {
		duration: 300		  
	}).tween('width', theWidth);
}

////////////////////////////////Subsections

var thePage = $$('body').get('id'); 
var theOpenMenu = 0;

if (thePage == "search-moco") theOpenMenu = 0;
else if (thePage == "design-moco") theOpenMenu = 1;
else if (thePage == "art-moco") theOpenMenu = 2;
else if (thePage == "fresh-moco") theOpenMenu = 3;
else theOpenMenu = 1;

/********************************************
* KRIS-3:
 * Added the line below
 * When you click on the toggle link, we want to cancel its default action.
 ********************************************/
// $$('a.toggle').addEvent('click', function(e) { e.preventDefault(); });

var myAccordion = new Accordion($('menu-slide'), 'a.toggle', 'div.subsection', {		
	display: theOpenMenu,
    alwaysHide: true,
	opacity: 0
});

////////////////////////////////Submenus

var buttons = $('menu-slide').getElements('.subsection-button');

buttons.each(function(item, index){
	var theSub = item.getElements('div.submenu-container');
    item.addEvents({
		'mouseenter': function(e){
			e.stop();
			tweenMenu('410px')
			theSub.setStyle('visibility', 'visible');
		},
		'mouseleave': function(e){
			e.stop();
			theSub.setStyle('visibility', 'hidden');
			tweenMenu('150px')
		}
	});
});

									 
////////////////////////////////The Likes and Follow labels		

var likes= $$('.like');
var likes_over= $$('.likes-over');
var likes_qty= $$('.likes-qty');
var likes_end = likes.length;
var i=0;

likes.each(function(item, index){
    item.addEvents({			  
		'mouseenter': function(e){
		e.stop();
		openLike(item)
		},
		'mouseleave': function(e) {
		e.stop();
		closeLike(item)
		}
	});
	
	//
    // Absolute position right has issues when scrolling in IE, so we'll use it's math for the position
    // then reassign position
	//
	item.setStyles({ right: "auto", left: item.getPosition(item.getParent()).x + "px" });
});

var openLike = function(currentLike){
	var qty = currentLike.getElements('span.likes-qty');
	var over = currentLike.getElements('span.likes-over');
	//currentLike.set('tween', {
//		duration: 150,
//		onComplete: function(){	
		qty.tween('display', 'none');	
		over.tween('display', 'block');
	//	} 
//	}).tween('width', 39);	
}

var closeLike = function(currentLike){
	var qty = currentLike.getElements('span.likes-qty');
	var over = currentLike.getElements('span.likes-over');
	//currentLike.set('tween', {
//		duration: 150
//	}).tween('width', 16);
	over.tween('display', 'none');
	qty.tween('display', 'block');	
}

////////////////////////////////Simple Mouse Over

var swap = new MooSwap('img.over', { imgHoverPrefix:'_over' });


////////////////////////////////Horizontal and vertical layouts

var loadNav = function(theDirection) {
	var theStylesheet = '/styles/style-'+theDirection+'.css'
	//var theNavfile = '/scripts/nav-'+theDirection+'.js'
	var css = new Asset.css(theStylesheet);
	//var js = new Asset.javascript(theNavfile);
    setTimeout(function() { selectors = getSelectors(); }, 1000);
};

var getSelectors = function() {
    scrollFx.set(0,0);
    return $$('.selector').map(function(item, index){
        return (layout == 'verti' ? $(item).getPosition().y : $(item).getPosition().x);
    });        
};

$$('.btn-layout').each(function(el) {
	   //add a click  event to add the stylesheet
	   el.addEvent('click', function() {
	  //get the file. file is based on the anchor's rel attribute
		var theChosenDirection = el.get('rel');
		layout = theChosenDirection;
		currentPosition = 0;
		var layoutCookie  = Cookie.write('layout', theChosenDirection, { duration: 365, path: '/' });
		loadNav(theChosenDirection);
	});
});

});




var layout  = Cookie.read('layout');
if (layout && layout == 'verti') {
	var theStylesheet = '/styles/style-'+layout+'.css'
	var theNavfile = '/scripts/nav-'+layout+'.js'
	var css = new Asset.css(theStylesheet);
	var js = new Asset.javascript(theNavfile);
}

function setIncludeBlogs(f) {
	var value = '';
	if (f.design.checked)	{
		value = value + f.design.value;
	}
	if (f.art.checked)	{
		if (value) value = value + ',';
		value = value + f.art.value;
	}
	if (f.fresh.checked)	{
		if (value) value = value + ',';
		value = value + f.fresh.value;
	}
	f.IncludeBlogs.value = value;
}


