jQuery(document).ready(function(){


	// COOKIES FOR LAYOUT SWITCHER
	jQuery.cookie = function (name, value, options) {
	    if (typeof value != 'undefined') {
	        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();
	        }
	        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 {
	        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]);
	                if (cookie.substring(0, name.length + 1) == (name + '=')) {
	                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
	                    break;
	                }
	            }
	        }
	        return cookieValue;
	    }
	};


	// LAYOUT SWITCHER
    jQuery("#switcher span").click(function () {
        jQuery("#content").fadeOut("fast", function () {
            jQuery("body.switchable").toggleClass("thumb-view list-view");
            jQuery(this).fadeIn("fast");
            jQuery(".not-singular:not(.thumb-slideshow) img.thumbnail").lazyload(); // requires lazyload.js
        });
        jQuery.cookie('view_State', jQuery('body').is('.list-view') ? 'thumbs' : 'list', {
            expires: 7
        });
    });
    var view_State = jQuery.cookie('view_State');
    if (view_State == 'list') {
        jQuery("body.switchable").addClass("list-view");
        jQuery("body.switchable").removeClass("thumb-view");
    };
    if (view_State == 'thumbs') {
        jQuery("body.switchable").addClass("thumb-view");
        jQuery("body.switchable").removeClass("list-view");
    };


    // LAZYLOAD FOR THUMBS
    // requires lazyload.js
	// can't use lazyload with roundimg.js
	jQuery(".not-singular:not(.thumb-slideshow) img.thumbnail").lazyload({ effect: "fadeIn", threshold: 100 });


    // Add/remove hover class
    jQuery(".hentry").hover(function () {
		jQuery(this).addClass('hover');
		jQuery(this).children(".inner").fadeIn();
	}, function () {
		jQuery(this).removeClass('hover');
		jQuery(this).children(".inner").fadeOut();
	});


	// DROPDOWN MENU WITH FADE
	jQuery(".dropdown ul,.slide-menu .slide").each(
	function (i) { // Preserves the mouse-over on top-level menu elements when hovering over children
	    jQuery(this).hover(
	    function () {
	        jQuery(this).parent().find("a").slice(0, 1).addClass("active");
	    }, function () {
	        jQuery(this).parent().find("a").slice(0, 1).removeClass("active");
	    });
	});
	jQuery(".dropdown ul").parent().find("a:first").append("<span class='darr'>&nbsp;&nbsp;&darr;</span><span class='rarr'>&nbsp;&nbsp;&rarr;</span>").addClass("parent");
	jQuery(".fadein ul").css({
	    display: "none"
	}); // Opera Fix
	jQuery(".fadein li").hover(
	function () {
	    jQuery("ul:first", this).css({
	        visibility: "visible",
	        display: "none"
	    }).fadeIn(400);
	}, function () {
	    jQuery("ul:first", this).css({
	        visibility: "hidden"
	    });
	});
	jQuery(this).find(".sub").stop().fadeTo('fast', 1).show();
	if (jQuery.browser.msie && (jQuery.browser.version < 7)) { // IE6 Fix: Drop-down fix due to lack of support for :hover on list elements
	    jQuery(".dropdown").each(
	    function (i) {
	        jQuery("li", this).hover(
	        function () {
	            jQuery(this).addClass("ie6hover");
	        }, function () {
	            jQuery(this).removeClass("ie6hover");
	        });
	    });
	}


	// SLIDEDOWN MENU
	jQuery(".slide-menu li").hover(
	function () {
	    jQuery("ul", this).stop("true", "true").slideDown("slow");
	}, function () {
	    jQuery("ul", this).slideUp();
	});


	// SLIDE TOGGLE (an anchor tag with class '.toggle' will expand it's href target)
	jQuery('.toggle').bind('click', function() {
		var maketoggle = jQuery(this).attr('href');
		jQuery(maketoggle).slideToggle(300);
		return false;
	});


	// SMOOTH SCROLL
	jQuery('a[href*=#]').click(function () {
	    if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
	        var $target = jQuery(this.hash);
	        $target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) + ']');
	        if ($target.length) {
	            var targetOffset = $target.offset().top;
	            jQuery('html,body').animate({
	                scrollTop: targetOffset
	            }, 700);
	            return false;
	        }
	    }
	});
	jQuery(window).bind('scroll', function(){
		jQuery('#backtotop').toggle(jQuery(this).scrollTop() > 200);
	});


	// TABBER WIDGET
	var list = '<ul class="tabs clearfix">';
	jQuery('.tabber').find('h3.widget-title').each(function () {
	    var the_title = jQuery(this).html();
	    list += '<li><a href="#">' + the_title + '</a></li>';
	});
	list += '</ul>';
	jQuery('.tabber').prepend(list);
	jQuery(".tabber .tabs").tabs(".tabber .widget", { // requires jquerytools.js
	    effect: 'fade'
	});



//	// EQUAL HIEGHT #CONTENT AND #SIDEBAR COLUMS
//	if(jQuery('#sidebar').height() > jQuery('#content').height()) {
//		jQuery('#content').height(jQuery('#sidebar').height());
//	}
//	else {
//		jQuery('#sidebar').height(jQuery('#content').height());
//	}



	// RANDOMIZE PLUGIN
    jQuery.fn.randomize = function (childElem) {
        return this.each(function () {
            var jQuerythis = jQuery(this);
            var elems = jQuerythis.find(childElem);
            elems.sort(function () {
                return (Math.round(Math.random()) - 0.5);
            });
            jQuerythis.remove(childElem);
            for (var i = 0; i < elems.length; i++)
            jQuerythis.append(elems[i]);
        });
    }

    //RANDOMIZE (ADS)
	jQuery(".ads-inside.random").randomize("a");


	// CONTACT FORM
	jQuery.fn.exists = function () { // Check if element exists
	    return jQuery(this).length;
	}
	jQuery('.dt-contactform').submit(function () {
	    var cf = jQuery(this);
	    cf.prev('.alert').slideUp(400, function () {
	        cf.prev('.alert').hide();
	        cf.find('.submit').after('').attr('disabled', 'disabled');
	        jQuery.post(ajaxurl, {
	            name: cf.find('.dt-name').val(),
	            email: cf.find('.dt-email').val(),
	            subject: cf.find('.dt-subject').val(),
	            comments: cf.find('.dt-comments').val(),
	            verify: cf.find('.dt-verify').val(),
	            action: 'dt_contact_form'
	        }, function (data) {
	            cf.prev('.alert').html(data);
	            cf.prev('.alert').slideDown('slow');
	            cf.find('img.loader').fadeOut('slow', function () {
	                jQuery(this).remove()
	            });
	            cf.find('.submit').attr('disabled', '');
	            if (data.match('success') != null) cf.slideUp('slow');
	        });
	    });
	    return false;
	});


});
