/**
 * @author sroucheray
 */


/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */


jQuery(document).ready(function(){
	
	
	jQuery.cookie = function(name, value, options) {
	    if (typeof value != 'undefined') { // name and value given, set cookie
	        options = options || {};
	        if (value === null) {
	            value = '';
	            options.expires = -1;
	        }
	        var expires = '';
	        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
	            var date;
	            if (typeof options.expires == 'number') {
	                date = new Date();
	                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
	            } else {
	                date = options.expires;
	            }
	            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
	        }
	        // CAUTION: Needed to parenthesize options.path and options.domain
	        // in the following expressions, otherwise they evaluate to undefined
	        // in the packed version for some reason...
	        var path = options.path ? '; path=' + (options.path) : '';
	        var domain = options.domain ? '; domain=' + (options.domain) : '';
	        var secure = options.secure ? '; secure' : '';
	        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
	    } else { // only name given, get cookie
	        var cookieValue = null;
	        if (document.cookie && document.cookie != '') {
	            var cookies = document.cookie.split(';');
	            for (var i = 0; i < cookies.length; i++) {
	                var cookie = jQuery.trim(cookies[i]);
	                // Does this cookie string begin with the name we want?
	                if (cookie.substring(0, name.length + 1) == (name + '=')) {
	                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                    break;
	                }
	            }
	        }
	        return cookieValue;
	    }
	};

    PeriscopeCarousel = new Object();
    PeriscopeCarousel.sliderStage = jQuery('#slider-stage');
    PeriscopeCarousel.sliderList = jQuery('#slider-list');
	
	//Set default style
	var userStyle = jQuery.cookie('by_periscope_style');
	if(userStyle){
		jQuery("#switcher").attr('href', userStyle);
	}else{
		jQuery("#switcher").attr('href', jQuery("#switcher").attr('class'));
	}
	
	
	if (PeriscopeCarousel.sliderStage && PeriscopeCarousel.sliderList) {
		
		var increment = 128;
		
		
		PeriscopeCarousel.elmnts = jQuery(PeriscopeCarousel.sliderList).children();
		PeriscopeCarousel.numElmts = PeriscopeCarousel.elmnts.length;
		PeriscopeCarousel.sizeFirstElmnt = increment;
		PeriscopeCarousel.shownInViewport = Math.round(jQuery(PeriscopeCarousel.sliderStage).width() / PeriscopeCarousel.sizeFirstElmnt);
		
		PeriscopeCarousel.firstElementOnViewPort = 1;
		PeriscopeCarousel.isAnimating = false;
		
		//Enable loop adding element add the end of the list
		for (i = 0; i < PeriscopeCarousel.shownInViewport; i++) {
			jQuery(PeriscopeCarousel.sliderList).css('width',(PeriscopeCarousel.numElmts+PeriscopeCarousel.shownInViewport)*increment + "px");
			jQuery(PeriscopeCarousel.sliderList).append(jQuery(PeriscopeCarousel.elmnts[i]).clone());
		}
		
		jQuery("#slider-list > .theme").click(function(event){
			var path = "http://www.byperiscope.com/wp-content/themes/freshnews/styles/switcher/";
			//console.log(jQuery("#switcher").attr('href'));
			jQuery("#switcher").attr('href', path+jQuery(this).attr('id'));
			//console.log(jQuery("#switcher").attr('href'));
			jQuery.cookie('by_periscope_style', path+jQuery(this).attr('id'), { expires: 1, path: '/' });
			event.preventDefault();
			
		});
		
		//-------------------------------------
		// EVENTS for the button "previous"
		jQuery('#previous').click(function(event){
			if (!PeriscopeCarousel.isAnimating) {
				if (PeriscopeCarousel.firstElementOnViewPort == 1) {
					jQuery(PeriscopeCarousel.sliderList).css('left', "-" + PeriscopeCarousel.numElmts * PeriscopeCarousel.sizeFirstElmnt + "px");
					PeriscopeCarousel.firstElementOnViewPort = PeriscopeCarousel.numElmts;
				}
				else {
					PeriscopeCarousel.firstElementOnViewPort--;
				}
				
				jQuery('#slider-list').animate({
					left: "+=" + increment,
					y: 0,
					queue: true
				}, "swing", function(){PeriscopeCarousel.isAnimating = false;});
				PeriscopeCarousel.isAnimating = true;
			}
			
		});
		
		//-------------------------------------
		// EVENTS for the button "next"
		jQuery('#next').click(function(event){
			if (!PeriscopeCarousel.isAnimating) {
				if (PeriscopeCarousel.firstElementOnViewPort > PeriscopeCarousel.numElmts) {
					PeriscopeCarousel.firstElementOnViewPort = 2;
					jQuery(PeriscopeCarousel.sliderList).css('left', "0px");
				}
				else {
					PeriscopeCarousel.firstElementOnViewPort++;
				}
				jQuery('#slider-list').animate({
					left: "-=" + increment,
					y: 0,
					queue: true
				}, "swing", function(){PeriscopeCarousel.isAnimating = false;});
				PeriscopeCarousel.isAnimating = true;
			}
		})
	}
    
});