/* 
 * Template Name: Doma
 * Created date: 2010.11.5
 * Author: Olziibat
 * Mail: olziinaa@gmail.com
 */


$(document).ready(function() {

    // STYLE SWITCH FUNCTION
    $('.styleswitch').click(function()
    {
        switchStylestyle(this.getAttribute("rel"));
        return false;
    });
    var c = readCookie('style');
    if (c) switchStylestyle(c);
    
    // JCYCLE SLIDESHOW FUNTION
    $('.jcycle-slideshow').cycle({
        });

    // JCYCLE PAGER FUNCTION
    $(function() {
        $('#jcycle-slideshow').after('<div id="jcycle-pager" class="jcycle-pager">').cycle({
            pager:  '#jcycle-pager',
            before: function() {
                if (window.console) console.log(this.src);
            }
        });
    });

    // JQUERY SPOTLIGHT FUNTION
    $('.spotlight').bind('mouseover', function () {
        $(this).spotlight({
            exitEvent: 'mouseover',
            speed: 100
        });
    });

    // NIVO SLIDER FUNCTION
    $('.nivo-slideshow').nivoSlider(
    {
        effect:'random', //Specify sets like: 'fold,fade,sliceDown'
        slices:15,
        animSpeed:500, //Slide transition speed
        pauseTime:3000,
        startSlide:0, //Set starting Slide (0 index)
        directionNav:true, //Next & Prev
        directionNavHide:true, //Only show on hover
        controlNav:true, //1,2,3...
        controlNavThumbs:false, //Use thumbnails for Control Nav
        controlNavThumbsFromRel:true, //Use image rel for thumbs
        controlNavThumbsSearch: '.jpg', //Replace this with...
        controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
        keyboardNav:true, //Use left & right arrows
        pauseOnHover:true, //Stop animation while hovering
        manualAdvance:false, //Force manual transitions
        captionOpacity:0.6, //Universal caption opacity
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, //Triggers after all slides have been shown
        lastSlide: function(){}, //Triggers when last slide is shown
        afterLoad: function(){} //Triggers when slider has loaded
    }
    );

    // KWICK SLIDER FUNCTION
    $('#kwicks').kwicks({
        duration: 200,
        easing: 'linear',
        max: 850,
        autoplay: true
    });

    // ROUND ABOUT SLIDER FUNCTION
    $('ul#roundabout').roundabout(
    {
        easing: 'swing',
        duration: 500,
        startingChild: 0,
        shape: 'figure8'

    });

    // ROUND ABOUT SLIDER AUTOPLAY FUNTION
    var interval;

    $('ul#roundabout').hover(
        function() {
            // oh no, it's the cops!
            clearInterval(interval);
        },
        function() {
            // false alarm: PARTY!
            interval = startAutoPlay();
        }
        );

    // let's get this party started
    interval = startAutoPlay();

    function startAutoPlay() {
        return setInterval(function() {
            $('ul#roundabout').roundabout_animateToPreviousChild(500, 'swing');
        }, 5000);
    };

    // IMAGE OVERLAY FUNCTION
    imgIconOverlay(jQuery);

    // JQUERY UI FUNCTIONS

    // JQUERY UI ACCORDION
    $( "#accordion" ).accordion({
        autoHeight: false
    });

    // JQUERY UI TABS
    $( "#tabs" ).tabs({
        fx: {
            height: 'slow',
            opacity: 'toggle',
            duration: 'fast'
        }
    });

    // JQUERY TOGGLE FUNTION
    $(".toggle_title").toggle(
        function(){
            jQuery(this).addClass('toggle_active');
            jQuery(this).siblings('.toggle_content').slideDown("fast");
        },
        function(){
            jQuery(this).removeClass('toggle_active');
            jQuery(this).siblings('.toggle_content').slideUp("fast");
        }
        );

    
    // IMAGE PRELOADER FUNCTIONS
    jQuery('.preload').preloadImages({
        showSpeed: 1000,   // length of fade-in animation, 500 is default
        easing: 'easeOutQuad'   // optional easing, if you don't have any easing scripts - delete this option
    });
    
    // SUPER FISH MENU FUNCTION
    $(function(){
        $('#menu-list').superfish();
    });

    // TOOL TIP FUNCTION
    if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) {
    // IE 6 sucks so the effect doesn't work at all and that's why we skip it here
    // we coluld do something different for IE 6 in this area if we wanted
    }
    else {
        $(function(){
            $(".tool-tip").tipTip();
        });
    };

    // PORTFOLIO FILTER FUNCTION
    $('#portfolio-list').filterable();

    //$("#contact-form").validationEngine();
    
    // FANCYBOX FUNCTIONS
    // -------------------------------------------------------------------


    $('a[href$="#popup"]').addClass('zoom').each( function() {
        $(this).attr('href', this.href.replace('#popup',''));
    });

    // FANCYBOX FOR YOUTUBE
    $("a.zoom[href*='http://www.youtube.com/watch?']").each( function() {
        $(this).addClass('fancyYouTube').removeClass('zoom');
    });

    // FANCYBOX FOR VIMEO
    $("a.zoom[href*='http://www.vimeo.com/']").each( function() {
        $(this).addClass('fancyVimeo').removeClass('zoom');
    });

    var overlayColor = '#000000';
    
    // FANCYBOX DEFAULT
    $('a.zoom').fancybox({
        'titlePosition': 'outside',
        'overlayOpacity': 0.9,
        'overlayColor': overlayColor
    });

    // YOUTUBE
    $('a.fancyYouTube').click(function() {
        $.fancybox({
            'overlayOpacity': 0.9,
            'overlayColor': overlayColor,
            'title': this.title,
            'href': this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
            'type': 'swf',
            'titlePosition': 'outside',
            'swf': {
                'wmode': 'transparent',
                'allowfullscreen'	: 'true'
            } // <-- flashvars
        });
        return false;
    });

    // VIMEO
    $("a.fancyVimeo").click(function() {
        $.fancybox({
            'overlayOpacity': 0.9,
            'overlayColor': overlayColor,
            'title': this.title,
            'href': this.href.replace(new RegExp("([0-9])","i"),'moogaloop.swf?clip_id=$1'),
            'titlePosition': 'outside',
            'type': 'swf'
        });
        return false;
    });
});



// RECPLACING FONTS

//Cufon.replace('h1, h2, h3, h4, h5, h6, p.cufon');
//Cufon.replace('#logo-name, .dropcap, #logo-description,.jcycle-button, ul#menu-list li a, ul#menu-game li a, div.jcycle-slideshow-item p, #top-action-text h1, #top-action-text span, .day, .month, .slogan-text, #top-action-button a, .home-content table.pricing tr th, .err-number, .not-found-desc');


// IMAGE PRELOADER

(function($){
    $.fn.preloadImages = function(options){

        var defaults = {
            showSpeed: 'fast',
            easing: 'easeOutQuad'
        };

        var options = $.extend(defaults, options);

        return this.each(function(){
            var container = $(this);
            var image = container.find('img');

            $(image).css({
                "visibility": "hidden",
                "opacity": "0"
            });
            $(image).bind('load error', function(){
                $(this).css({
                    "visibility": "visible"
                }).animate({
                    opacity:"1"
                }, {
                    duration:options.showSpeed,
                    easing:options.easing
                }).parent(container).removeClass('preload');
            }).each(function(){
                if(this.complete || ($.browser.msie && parseInt($.browser.version) == 6)) {
                    $(this).trigger('load');
                }
            });
        });
    }
})(jQuery);


// IMAGE OVERLAY

function imgIconOverlay($) {


    // This will select the items which should include the image overlays
    $("a.imgMedium:not(.noIcon)").each(function(){
        var	ctnr = $(this);
        // insert the overlay image
        if (ctnr.children('img')) {
            if (ctnr.hasClass('iconPlay')) {
                ctnr.children('img').after($('<div class="imgOverlay symbolPlay"></div>'));
            } else if  (ctnr.hasClass('iconDoc')) {
                ctnr.children('img').after($('<div class="imgOverlay symbolDoc"></div>'));
            } else {
                ctnr.children('img').after($('<div class="imgOverlay symbolZoom"></div>'));
            }
        }

        var overImg = ctnr.children('.imgOverlay');

        if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 6) {
            // IE sucks at fading PNG's with gradients so just use show hide
            overImg.css('visibility','hidden'); // not visible to start

            ctnr.hoverIntent(
                function(){
                    overImg.css('visibility','visible');
                },		// mouseover
                function(){
                    overImg.css('visibility','hidden');
                }		// mouseout
                );
        } else {
            // make sure it's not visible to start
            overImg.css('opacity',0);

            ctnr.hoverIntent(
                function(){
                    overImg.animate({
                        'opacity':'1'
                    },100,'swing');
                },		// mouseover
                function(){
                    overImg.animate({
                        'opacity':'0'
                    },100,'swing');
                }		// mouseout
                );
        }
    });
}


// STYLE SWITCH FUNCTIONS

function switchStylestyle(styleName)
{
    $('link[@rel*=style][title]').each(function(i)
    {
        this.disabled = true;
        if (this.getAttribute('title') == styleName) this.disabled = false;
    });
    createCookie('style', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function eraseCookie(name)
{
    createCookie(name,"",-1);
}

