// IE checker
function gkIsIE() {
	var myNav = navigator.userAgent.toLowerCase();
	return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}
//
window.addEvent('domready', function() {
	// smooth anchor scrolling
	jQuery('a[href^="#"]').on('click', function (e) {
		e.preventDefault();
		if(this.hash !== '') {
			var target = jQuery(this.hash);
			jQuery('html, body').stop().animate({
				'scrollTop': target.offset().top
			}, 1000, 'swing', function () {
				window.location.hash = target.selector;
			});
		}
	});
	// style area
	if(jQuery('#gkStyleArea').length > 0){
		jQuery('#gkStyleArea').find('a').each(function(i, element){
			jQuery(element).click(function(e){
				e.preventDefault();
				e.stopPropagation();
				changeStyle(i+1);
			});
		});
	}
	// K2 font-size switcher fix
	if(jQuery('#fontIncrease').length > 0 && jQuery('.itemIntroText').length > 0) {
		jQuery('#fontIncrease').click(function() {
			jQuery('.itemIntroText').attr('class', 'itemIntroText largerFontSize');
		});
		
		jQuery('#fontDecrease').click( function() {
			jQuery('.itemIntroText').attr('class', 'itemIntroText smallerFontSize');
		});
	}
	// Equal columns
	gkEqualColumns();
	// Floating sidebar
	if(jQuery('#gk-right-top')) {
		var top_area = jQuery('#gk-right-top');
		var top_area_bg = jQuery('#gk-right-top-bg');
		var win = jQuery(window);
		var body = jQuery(document.body);
		
		jQuery(window).scroll(function() {
			if(win.width() >= body.attr('data-tablet-width')) {
				var scrolled = win.scrollTop();
				top_area.css('top', scrolled + 'px');
				top_area_bg.css('top', scrolled + 'px');
			} else {
				top_area.css('top', '0px');
			}
		});
	}
	// Login popup
	if(jQuery('#gk-login-btn')) {
		var login_btn = jQuery('#gk-login-btn');
		var sidebar = jQuery('#gk-right');
		var popup = jQuery('#gkPopupLogin');
		
		login_btn.click(function(e) {
			e.preventDefault();
			
			if(login_btn.hasClass('popup-open')) {
				sidebar.removeClass('popup-open')
				login_btn.removeClass('popup-open');
				
				setTimeout(function() {
					popup.css('display', 'none');
				}, 350);
			} else {
				popup.css('display', 'block');
				
				if(jQuery(window).width() <= jQuery(document.body).attr('data-tablet-width')) {
					popup.css('top', '100px');
				} else {
					popup.css('top', (jQuery(window).scrollTop() + 100) + "px");
				}
				
				setTimeout(function() {
					sidebar.addClass('popup-open');
					login_btn.addClass('popup-open');
				}, 50);
			}
		});
	}
	// search
	var searchbox = jQuery('#gk-search .inputbox');
	if(searchbox.length) {
		searchbox.focus(function() {
			var input = jQuery('#gk-search');
			input.addClass('search-active');
		});
		
		searchbox.blur(function() {
			var input = jQuery('#gk-search');
			input.removeClass('search-active');
		});
	}
});

jQuery(window).load(function() {
	gkEqualColumns();
});

jQuery(window).resize(function() {
	gkEqualColumns();
});

function gkEqualColumns() {
	if(jQuery(window).width() > jQuery(document.body).attr('data-tablet-width')) {
		var content = jQuery('#gk-content');
		var sidebar = jQuery('#gk-right');
		
		if(!jQuery(document.body).hasClass('fixed-content')) {
			content.css('min-height', 0);
			
			if(content.outerHeight() < jQuery(window).height()) {
				content.css('min-height', jQuery(window).height() + "px");
			}
			
			if(
				content.outerHeight() < sidebar.outerHeight() &&
				jQuery(window).height() < sidebar.outerHeight()
			) {
				content.css('min-height', sidebar.outerHeight() + "px");
			}
		}
	}
}

// function used to maintain fixed content on frontpage
jQuery(document).ready(function() {
	if(jQuery('#gk-right') && jQuery(document.body).hasClass('fixed-content')) {
		function gkFixedContent() {
			if(jQuery(window).width() <= jQuery(document.body).attr('data-tablet-width')) {
				jQuery('#gk-right').css('margin-top', jQuery('#gk-content').outerHeight() + "px");	
			} else {
				jQuery('#gk-right').css('margin-top', '0px');
			}
		}
		
		gkFixedContent();
		
		jQuery(window).resize(function() {
			gkFixedContent();
		});
	}
});

// Function to change styles
function changeStyle(style){
	// cookie function
	jQuery.cookie = function (key, value, options) {
    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);
        if (value === null || value === undefined) {
            options.expires = -1;
        }
        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }
    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
	};

	var file1 = $GK_TMPL_URL+'/css/style'+style+'.css';
	var file2 = $GK_TMPL_URL+'/css/typography/typography.style'+style+'.css';
	jQuery('head').append('<link rel="stylesheet" href="'+file1+'" type="text/css" />');
	jQuery('head').append('<link rel="stylesheet" href="'+file2+'" type="text/css" />');

	jQuery.cookie('gk_writer_j30_style', style, { expires: 365, path: '/' });
}
