/*
 * @Coded İn GEN3 CREATİVE studios
 * @Author: GEN3 CREATİVE
 * @Project Start: 01.12.2011
 * büyük hendek caddesi no: 17 kat: 5 galata / beyoğlu /istanbul
 * t.: +90 (212) 243 80 09  f.: +90 (212) 243 70 78  w.: www.gen3.com.tr
 */

$(document).ready(function () {
    /*
     * Ana Menü Hover Olayı
     */
    $("div.header > div.menu > div.menu_item_x").each(function () {
        var $href = $(this);
        $href.hover(
            function () {
                $href.addClass("menu_item_active");
                $href.children("div.sub_menu_container").fadeIn(300, function () {

                });
            },
            function () {
                $(this).removeClass("menu_item_active");
                $(this).children("div.sub_menu_container").fadeOut(50);
                $(this).find("a.read_more").fadeOut("fast");
            }
        );
    });
    $(".sub_menu_item").live({
        mouseenter:function () {
            $(this).parents(".menu_item:eq(0)").css("cursor", "auto");
            $(this).animate({
                backgroundColor:"#eef7d6",
                border:"1px solid #e5eecb"
            }, 400);
            $(this).find("a.read_more").fadeIn("fast");
        },
        mouseleave:function () {
            $(this).parents(".menu_item:eq(0)").css("cursor", "pointer");
            $(this).animate({
                backgroundColor:"#ffffff",
                border:"1px solid #ffffff"
            }, 10);
            $(this).find("a.read_more").fadeOut("fast");
        }
    });
    $("div.header > div.menu > div.menu_item_x > menu_item_center > a").each(function () {
        var $href = $(this);
        $href.click(function (e) {
            e.preventDefault();
        });
    });
    $("a.tanitim").click(function (e) {
        e.preventDefault();

        var $content = '<a href="' + $(this).attr("href") + '" id="tanitim_filmi" style="width:640px; height:480px;">';
        $(this).flayer($content, "", "", {
            flayerCss:{
                height:480,
                width:640,
                backgroundColor:"#000000"
            },
            headerCss:{
                height:0,
                width:640
            },
            closeButtonCss:{
                height:0,
                width:20
            },
            contentCss:{
                height:480,
                width:640
            },
            footerCss:{
                height:0,
                width:640
            },
            footerLeftCss:{
                height:0,
                width:240
            },
            footerRightCss:{
                height:0,
                width:240
            }
        });
        $f("tanitim_filmi", sitedir + "/upload/flash/flowplayer-3.2.7.swf");

    });
    $("div.medication_image").hover(function () {
        $(this).css({backgroundImage:"url(" + themedir + "/images/ilaclama_raporu_hover.png)"});
    }, function () {
        $(this).css({backgroundImage:"url(" + themedir + "/images/ilaclama_raporu.png)"});
    });
    $("div.medication_image").click(function (e) {
        e.preventDefault();
        $("div.medicationConatainer").fadeIn("slow");
        $("div.medicationContent").cycle({
            fx:'fade',
            speed:'slow',
            timeout:0,
            next:'.medication_button_right',
            prev:'.medication_button_left',
            slideExpr:'img',
            before:onBefore

        });
        function onBefore(curr, next, opts, fwd) {

            var $width = $(next).width();
            var $height = $(next).height();
            var $docHeight = $("body").height();
            var $docwidth = $("body").width();

            $("div.medicationConatainer").animate({
                height:$docHeight,
                width:$docwidth
            }, 0, "swing");
            $("div.medication").animate({
                width:$width + 320,
                height:$height + 176
            }, 400);
            $("div.medicationHeader").animate({
                width:$width + 320
            }, 400);
            $("div.medicationFooter").animate({
                width:$width + 320
            }, 400);
            $("div.medicationContent").animate({
                width:$width,
                height:$height
            }, 400);
            $("div.medicationRight").animate({
                height:$height
            }, 400);
            $("div.medicationLeft").animate({
                height:$height
            }, 400);
        }

        $("div.medicationConatainer").bind("click", function () {
            $("div.medicationConatainer").fadeOut("fast", function () {
                $("div.flayerConatainer").unbind("click");
            });
        });
        $("div.medication_button_right, div.medication_button_left").bind("click", function () {
            //$("div.medicationConatainer").fadeOut("fast", function () {
            //    $("div.flayerConatainer").unbind("click");
            //});
        });
        $(document).keydown(function (e) {
            if (e.which == 27) {
                $("div.medicationConatainer").fadeOut("fast", function () {
                    $("div.medicationConatainer").unbind("click");
                });
            }
        });
        $("div.medication").click(function () {
            return false;
        });
    });
    $("body").mousemove(function (e) {
        var $pos = $("#parallax").attr("pos");
        var $order = $("#parallax").attr("order");
        var $orderA = $("#parallax").attr("orderA");
        var $par = $("#parallax").attr("par");
        var $yon = $("#parallax").attr("yon");
        $("#parallax").find("div").each(function () {
            var $index = $(this).index();
            $(this).css({zIndex:($index + 1) * 100});
        });
        var $u = $("#parallax > div").length;
        var $pageW = parseInt($(window).width());
        var $pageM = parseInt($pageW / 2);
        var $el = $("#parallax").find("div").length;
        $("#parallax").find("div").each(function () {
            var $index = $(this).index();
            var $movementX = e.pageX;
            if ($order && $order == 1) {
                if ($orderA == "asc") {
                    var $in = $u - $index;
                    if ($par && $par != 0) {
                        var $a = $par;
                    } else {
                        var $a = 20
                    }
                    if ($yon == "tek") {
                        var $ss = true;
                    } else {
                        var $ss = false;
                    }
                    if (($a * ($u - $index)) == 0) {
                        var $sort = $a;
                    } else {
                        var $sort = $a * ($u - $index);
                    }
                } else if ($orderA == "desc") {
                    if ($par && $par != 0) {
                        var $a = $par;
                    } else {
                        var $a = 10
                    }
                    var $in = $index + 1;
                    var $sort = $a * ($u + $index);

                }
                if ($ss) {
                    if ($index % 2 == 0) {
                        $(this).css("left", (($movementX) / ($in * $sort) + "px"));
                    } else {
                        $(this).css("left", (($movementX) / -($in * $sort) + "px"));
                    }
                } else {
                    if ($index % 2 == 1) {
                        $(this).css("left", (($movementX) / ($in * $sort) + "px"));
                    } else {
                        $(this).css("left", (($movementX) / -($in * $sort) + "px"));
                    }
                }

            } else if ($pos && $pos == 1) {
                if ($index % 2 == 0) {
                    $(this).css("left", (($movementX) / (($index + 0.5) * 50 )) + "px");
                } else {
                    $(this).css("left", (($movementX) / -(($index + 0.5) * 50)) + "px");
                }
            } else {
                if ($index % 2 == 0) {
                    $(this).css("left", (($movementX) / -(($index + 0.5) * 50)) + "px");
                } else {
                    $(this).css("left", (($movementX) / (($index + 0.5) * 50 )) + "px");
                }
            }

        });
    });

    $('.news').cycle({
        /*
         fx:     'scrollVert',
         speed: 0,
         timeout: 8000,
         */
        fx:      'scrollDown',
        speedIn:  400,
        speedOut: 200,
        easeIn:  'easeInCirc',
        easeOut: 'easeOutBounce',
        delay:   -8000,
        slideExpr: 'span',
        next:   'a.news_next',
        prev:   'a.news_prev'
    });


});

(function ($) {
    /*
     Webpage error details

     User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C)
     Timestamp: Mon, 19 Dec 2011 08:27:58 UTC


     Message: Expected identifier
     Line: 180
     Char: 15
     Code: 0
     URI: http://localhost/eventgarden/


     */
    $.fn.paradox = function (options) {
        options = $.extend({
            isOrdered:true,
            sortAsc:true,
            sortDesc:false,
            startFromOdd:true,
            startFromEven:false,
            animSpeed:20,
            holder:$('div')

        }, options);
        this.find('div').each(function () {
            var $i = $(this).index();
            $(this).css({zIndex:($i + 1) * 100});
        });
        $("body").mousemove(function (e) {
            var $pageW = parseInt($(window).width());
            var $pageM = parseInt($pageW / 2);
            this.find(options.holder).each(function () {
                var $index = $(this).index();
                var $movementX = e.pageX;
                if (options.isOrdered) {
                    if (options.sortAsc) {
                        var $in = $lenght - $index;
                        if ((options.animSpeed * ($lenght - $index)) == 0) {
                            var $sort = options.animSpeed;
                        } else {
                            var $sort = options.animSpeed * ($lenght - $index);
                        }
                    } else if (options.sortDesc) {

                        var $in = $index + 1;
                        var $sort = options.animSpeed * ($lenght + $index);

                    }
                    if (options.startFromOdd) {
                        if ($index % 2 == 0) {
                            $(this).css("left", (($movementX) / ($in * $sort) + "px"));
                        } else {
                            $(this).css("left", (($movementX) / -($in * $sort) + "px"));
                        }
                    } else if (options.startFromEven) {
                        if ($index % 2 == 1) {
                            $(this).css("left", (($movementX) / ($in * $sort) + "px"));
                        } else {
                            $(this).css("left", (($movementX) / -($in * $sort) + "px"));
                        }
                    } else {
                        if ($index % 2 == 1) {
                            $(this).css("left", (($movementX) / ($in * $sort) + "px"));
                        } else {
                            $(this).css("left", (($movementX) / -($in * $sort) + "px"));
                        }
                    }

                }
            });
        });
    };
})(jQuery);


(function ($) {
    $.fn.flayer = function (text, footerLeft, footerRight, options) {
        //set document width and height


        // default options
        options = $.extend({
            flayerCss:{},
            headerCss:{},
            closeButtonCss:{},
            contentCss:{},
            footerCss:{}

        }, options);
        var wHeight = $(window).height();
        var $flayerSistemi =
            '<div class="flayerConatainer">' +
                '<div class="flayer">' +
                '<div class="flayerHeader">' +
                '<span class="flayerClose"></span>' +
                '</div>' +
                '<div class="flayerContent">' +
                text +
                '</div>' +
                '<div class="flayerFooter">' +
                '<div class="flayerFooterLeft">' +
                footerLeft +
                '</div>' +
                '<div class="flayerFooterRight">' +
                footerRight +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';
        $("body").prepend($flayerSistemi);

        $(".flayer").css(options.flayerCss);
        $(".flayerHeader").css(options.headerCss);
        $(".flayerClose").css(options.closeButtonCss);
        $(".flayerContent").css(options.contentCss);
        $(".flayerFooter").css(options.footerCss);
        $(".flayerFooterLeft").css(options.footerLeftCss);
        $(".flayerFooterRight").css(options.footerRightCss);

        $("div.flayerConatainer").fadeIn("slow");
        $("span.flayerClose, div.flayerConatainer").bind("click", function () {
            $("div.flayerConatainer").fadeOut("fast", function () {
                $(this).remove();
                $("span.flayerClose, div.flayerConatainer").unbind("click");
            });
        });
        $(".flayer").css("margin-top", ((wHeight - $(".flayer").height()) / 2) + "px");
        $(document).keydown(function (e) {
            if (e.which == 27) {
                $("div.flayerConatainer").fadeOut("fast", function () {
                    $(this).remove();
                    $("span.flayerClose").unbind("click");
                });
            }
        });
        $("div.flayer").click(function () {
            return false;
        });

    };
})(jQuery);

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;
    }
};

(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--;) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)

jQuery.fn.onImagesLoaded = function (_cb) {
    return this.each(function () {

        var $imgs = (this.tagName.toLowerCase() === 'img') ? $(this) : $('img', this),
            _cont = this,
            i = 0,
            _done = function () {
                if (typeof _cb === 'function') _cb(_cont);
            };

        if ($imgs.length) {
            $imgs.each(function () {
                var _img = this,
                    _checki = function (e) {
                        if ((_img.complete) || (_img.readyState == 'complete' && e.type == 'readystatechange')) {
                            if (++i === $imgs.length) _done();
                        }
                        else if (_img.readyState === undefined) // dont for IE
                        {
                            $(_img).attr('src', $(_img).attr('src')); // re-fire load event
                        }
                    }; // _checki \\

                $(_img).bind('load readystatechange', function (e) {
                    _checki(e);
                });
                _checki({type:'readystatechange'}); // bind to 'load' event...
            });
        } else _done();
    });
};

function eventLoadImage (curr, next, opts) {
    var $m = curr.parentElement.children;
    var stack = [];
    for (var i = 0; i < $m.length; i++) {
        if($m.item(i).tagName == "IMG" ){
            //alert($m.item(i).src);
            var img = new Image();
            img.src = $m.item(i).src;
        }

        $(img).bind('load', function () {
            stack.push(this);
        });
    }


    if (opts.addSlide)
        while (stack.length)
            opts.addSlide(stack.pop());

}


