﻿jQuery.fn.exists = function() { return ($(this).length > 0); }

// ParseUrl
function parseUrl(data) {
    var e = /((http|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+\.[^#?\s]+)(#[\w\-]+)?/;
    if (data.match(e)) {
        return { url: RegExp['$&'],
            protocol: RegExp.$2,
            host: RegExp.$3,
            path: RegExp.$4,
            file: RegExp.$6,
            hash: RegExp.$7
        };
    }
    else {
        return { url: "", protocol: "", host: "", path: "", file: "", hash: "" };

    }
}

function setCurrent(file) {
    $(".menu li a[href^='" + file + "']").addClass("current");
}

function setInputFields(selector) {
    $(selector).each(function() {
    var input_title = $(this).attr("title");
        if ($(this).attr("value") == "")
            $(this).attr("value", input_title);
    });
    
}

function scrollAfter(curr, next, opts) {
    var index = opts.currSlide;
    $(opts.prev)[index == 0 ? 'hide' : 'show']();
    $(opts.next)[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function validate(selector) {
    $(selector).each(function() {
        var input_title = $(this).attr("title");
        if ($(this).attr("value") == input_title)
            $(this).attr("value", "");
    });
}

// OnLoad
$(function() {

    // Last-Child
    $(".shows-slide .nav li:last-child").addClass("last");
    $(".shows ul li:last-child").addClass("last");

    // MenuCurrent
    var url = parseUrl(document.location.href);
    if (url.file != "") setCurrent(url.file);

    // Input Focus out/in
    var input_fields = ".newsletter span input, .search input.field, .register span input, .contact span input, .contact span textarea"
    setInputFields(input_fields);
    $(input_fields).focusin(function() {
        var input_title = $(this).attr("title");
        if ($(this).attr("value") == input_title)
            $(this).attr("value", "");
    });
    $(input_fields).focusout(function() {
        var input_title = $(this).attr("title");
        if ($(this).attr("value") == "") {
            $(this).attr("value", input_title);
        }
    });
    
    //  > jScroller
    if ( $(".now-playing").exists() ){
        $jScroller.add(".now-playing",".now-playing span","right",1,true);
        $jScroller.start();
	}
	    
	// csScroll > Cycle
    if ( $("#csScroll").exists() ){
		if ( $("#csScroll ul").length == 1 ){
			$("#csScroll ul").css("display", "block");
			$("#csNav").css("display", "none");
		} else {
			$('#csScroll').cycle({
				fx: 'fade', timeout: 0, speed: 500, next: '#Next', prev: '#Prev', after: scrollAfter, cleartypeNoBg: true
			});
		} 
	}
	

    // Ilusrtations
    var htmlHeight = $(document).height();

    $('<div class="illustration-top-right"></div><div class="illustration-bottom-left" style="top: ' + (htmlHeight - 475) + 'px"></div>').appendTo("body").fadeIn(1500);
    $('<div class="illustration-top-left"></div>').prependTo("#grid").fadeIn(1500);
    $('<div class="illustration-middle-bottom-left"></div><div class="illustration-middle-bottom-line"></div>').prependTo(".main-content").fadeIn(1500);
    $('<div class="illustration-footer-right"></div><div class="illustration-footer-middle"></div>').prependTo(".footer").fadeIn(1500);

    // $(window).resize(function(){
    // 	var htmlHeight = $(document).height();
    // 	$("div.illustration-bottom-left").css("top", htmlHeight - 475);
    // });

    // ShowsSlide > Cycle
    if ($('#showsSlide').exists()) {
        $('ul.nav li a').click(function() {
            document.location.href = $(this).attr('href');
        });
        $('#showsSlide').cycle({
            fx: 'fade',
            speed: 100,
            timeout: 5000,
            cleartypeNoBg: true,
            pager: '#showsNavSlide',
            pagerEvent: 'mouseover',
            pagerAnchorBuilder: function(idx, slide) {
                return '#showsNavSlide li:eq(' + idx + ') a';
            }
        });

        if ($("#showsSlide div").length == 1) {
            $("#showsSlide div").css("display", "block");
            $("#showsSlide div img, #showsSlide div span, #showsSlide div a").css("right", "0");
        }
    }

    // MoreImages > FancyBox
    if ($('#iGallery').exists()) {
        $("#iGallery a").fancybox();
    }

    // Video > FancyBox
    if ($('#vGallery').exists()) {
        $("#vGallery a").fancybox();
    }

    //share button
    if (url.file.toLowerCase() != 'search.aspx') {
        addShareHtml($('.editor-content'));
        //like button
        var like = '<iframe src="http://www.facebook.com/plugins/like.php?href=' + escape(document.location) + '&amp;layout=standard&amp;show_faces=true&amp;width=316&amp;action=like&amp;colorscheme=light&amp;height=80" scrolling="no" frameborder="0" style="border: none; overflow: hidden; width: 316px; height: 80px;" allowtransparency="true"></iframe>';
        $('.editor-content').append(like);
    }

});

function addShareHtml(jqobj) {
    jqobj.prepend('<div class="addthis_toolbox addthis_default_style">' +
                    '<a href="http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4c18cfeb45f54877" class="addthis_button_compact">שתף</a>' +
                    '<span class="addthis_separator">|</span>' +
                    '<a class="addthis_button_facebook"></a>' +
                    '<a class="addthis_button_myspace"></a>' +
                    '<a class="addthis_button_google"></a>' +
                    '<a class="addthis_button_twitter"></a>' +
                    '</div>' +
                    '<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4c18cfeb45f54877"></script>');
}





