/* IE6 flicker fix
-------------------------------------------------- */
try {
	document.execCommand("BackgroundImageCache", false, true);
} catch (err) { }


/*
* Author: Rob Reid
* CreateDate: 20-Mar-09
* Description: Little helper function to return details about IE 8 and its various compatibility settings either use as it is
* or incorporate into a browser object. Remember browser sniffing is not the best way to detect user-settings as spoofing is
* very common so use with caution.
*/
function IEVersion() {
	var _n = navigator, _w = window, _d = document;
	var version = "NA";
	var na = _n.userAgent;
	var ieDocMode = "NA";
	var ie8BrowserMode = "NA";
	// Look for msie and make sure its not opera in disguise
	if (/msie/i.test(na) && (!_w.opera)) {
		// also check for spoofers by checking known IE objects
		if (_w.attachEvent && _w.ActiveXObject) {
			// Get version displayed in UA although if its IE 8 running in 7 or compat mode it will appear as 7
			version = (na.match(/.+ie\s([\d.]+)/i) || [])[1];
			// Its IE 8 pretending to be IE 7 or in compat mode		
			if (parseInt(version) == 7) {
				// documentMode is only supported in IE 8 so we know if its here its really IE 8
				if (_d.documentMode) {
					version = 8; //reset? change if you need to
					// IE in Compat mode will mention Trident in the useragent
					if (/trident\/\d/i.test(na)) {
						ie8BrowserMode = "Compat Mode";
						// if it doesn't then its running in IE 7 mode
					} else {
						ie8BrowserMode = "IE 7 Mode";
					}
				}
			} else if (parseInt(version) == 8) {
				// IE 8 will always have documentMode available
				if (_d.documentMode) { ie8BrowserMode = "IE 8 Mode"; }
			}
			// If we are in IE 8 (any mode) or previous versions of IE we check for the documentMode or compatMode for pre 8 versions			
			ieDocMode = (_d.documentMode) ? _d.documentMode : (_d.compatMode && _d.compatMode == "CSS1Compat") ? 7 : 5; //default to quirks mode IE5				   			
		}
	}

	return {
		"UserAgent": na,
		"Version": version,
		"BrowserMode": ie8BrowserMode,
		"DocMode": ieDocMode
	}
}

/**
* Cookie plugin
*
* Copyright (c) 2006 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/

/**
* Create a cookie with the given name and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
*       used when the cookie was set.
*
* @param String name The name of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
*                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
*                             If set to null or omitted, the cookie will be a session cookie and will not be retained
*                             when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
*                        require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/

/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options = $j.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
			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(); // use expires attribute, max-age is not supported by IE
		}
		// NOTE Needed to parenthesize options.path and options.domain
		// in the following expressions, otherwise they evaluate to undefined
		// in the packed version for some reason...
		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 { // only name given, get cookie
		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]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};


function popup(mylink, windowname) {
    var w = 560;
    var h = 450;
    var left = (screen.width / 2) - (w / 2);
    var top = (screen.height / 2) - (h / 2);
    if (!window.focus) return true;
    window.open(mylink, windowname, 'width=' + w + ',height=' + h + ',scrollbars=no,resizable=no,location=no, top=' + top + ', left=' + left);

    return false;
}


function get_url_param(name) {
    var name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}

function resizeFrame(frame, padd) {
    var h = parseInt(frame.contentWindow.document.body.scrollHeight) + parseInt(padd);
    frame.style.height = h + 'px';
}

function reloadFrame(frameid) {
    var f = document.getElementById(frameid);
    f.src = f.src;
}

/* jQuery
-------------------------------------------------- */
var $j = jQuery.noConflict();

/*
var signOut = {
	init : function(){
		$j("div.loggedin p.exit a").click(this.clickIt);
	},
	clickIt : function(){
		$j(this).parents("div.loggedin").hide();
		$j("div.loggedout").fadeIn(400);
		return false;
	}
};
var closeSignoutMsg = {
	init : function(){
		$j("div.loggedout p.exit a").click(this.clickIt);
	},
	clickIt : function(){
		$j(this).parents("div.loggedout").slideUp("fast");
		return false;
	}
};
*/

function dataHover() {
	$j("table.data tr").hover(
		function(){
			$j(this).addClass("hover");
		},
		function(){
			$j(this).removeClass("hover");
		}
	);
}

var ExampleInputText = {
    b: null,
    init: function() {
//        if ((typeof (WebForm_OnSubmit) == 'function')) {
//            this.b = WebForm_OnSubmit;
//            WebForm_OnSubmit = this.destroy;
//        }
        $j("input.text[example!=''][focused!='true']").each(function(i) {
            if ($j(this).val() == "") {
                $j(this).addClass("water-marker-text");
                $j(this).val($j(this).attr("example"));
            }
        });
        $j("input.text[example!='']").focus(function() {
            if ($j(this).hasClass("water-marker-text"))
            {
                $j(this).removeClass("water-marker-text");
                $j(this).val("");
            }
        });
        $j("input.text[example!='']").blur(function() {
            if ($j(this).val() == "") {
                $j(this).addClass("water-marker-text");
                $j(this).val($j(this).attr("example"));
            }
        });
    },
    destroy: function() {
        $j("input.water-marker-text").each(function(i) {
            $j(this).val("");
        });
        if (this.b)
            this.b();
    }
}

//var treeTable = {
//    init: function() {
//        $j('.head').click(function() {
//            $j(this).nextAll('.child').toggleClass('hidden');
//            $j(this).toggleClass('expanded');
//            $j(this).toggleClass('collapsed');
//        });
//    }
//}

var _118NotepadFrame = {
    init: function() {
        if (get_url_param('118uk') == "true" || get_url_param('118UK') == "true") {
            $j("#expand-frame-link").toggle();
            $j("#collapse-frame-link").toggle();
            $j("#promo-text").toggle();
            $j(".spacer").toggle();
            //$j("#logo").attr("id","logo-small");
            $j(".plogo").addClass("plogo-small");
            $j(".plogo").removeClass("plogo");
            //$j("#header").attr("id","header-small");
            $j(".divheader").addClass("divheader-small");
            $j(".divheader").removeClass("divheader");
            $j("#frame-div").css("display", "block");
            $j("#frame-div").css("height", "auto");
        }
        $j("#expand-frame-link").click(this.expand);
        $j("#collapse-frame-link").click(this.collapse);
    },
    expand: function() {
        reloadFrame("frame118uk");
        $j(".spacer").toggle();
        //$j("#logo").attr("id","logo-small");
        $j(".plogo").addClass("plogo-small");
        $j(".plogo").removeClass("plogo");
        //$j("#header").attr("id","header-small");
        $j(".divheader").addClass("divheader-small");
        $j(".divheader").removeClass("divheader");
        $j("#expand-frame-link").toggle();
        $j("#promo-text").toggle();
        $j("#collapse-frame-link").toggle();
        $j("#frame-div").css("display", "none");
        $j("#frame-div").css("height", "auto");
        $j("#frame-div").slideDown(1000);
    },
    collapse: function() {
        $j(".spacer").toggle();
        //$j("#logo").attr("id","logo-small");
        $j(".plogo-small").addClass("plogo");
        $j(".plogo").removeClass("plogo-small");
        //$j("#header").attr("id","header-small");
        $j(".divheader-small").addClass("divheader");
        $j(".divheader").removeClass("divheader-small");
        $j("#expand-frame-link").toggle();
        $j("#promo-text").toggle();
        $j("#collapse-frame-link").toggle();
        $j("#frame-div").slideUp(1000);
    }
};

var gallery = {
	init: function() {
		$j('.gallery_demo_unstyled').addClass('gallery'); // adds new class name to maintain degradability

		$j('ul.gallery').galleria({
			history: false, // activates the history object for bookmarking, back-button etc.
			clickNext: true, // helper for making the image clickable
			insert: '#main_image', // the containing selector for our main image
			onImage: function(image, caption, thumb) { // let's add some image effects for demonstration purposes

				// fade in the image & caption
				if (!($j.browser.mozilla && navigator.appVersion.indexOf("Win") != -1)) { // FF/Win fades large images terribly slow

				}
				image.css('display', 'none').fadeIn(1000);
				caption.css('display', 'none').fadeIn(1000);

				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(500, 0.3);

				// fade in active thumbnail
				thumb.fadeTo('fast', 1).addClass('selected');

				// add a title for the clickable image
				if (this.clickNext)
					image.attr('title', 'Next image >>');
			},
			onThumb: function(thumb) { // thumbnail effects goes here
				// fetch the thumbnail container
				var _li = thumb.parents('li');

				// if thumbnail is active, fade all the way.
				var _fadeTo = _li.is('.active') ? '1' : '0.3';

				// fade in the thumbnail when finnished loading
				thumb.css({ display: 'none', opacity: _fadeTo }).fadeIn(1500);

				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast', 1); },
					function() { _li.not('.active').children('img').fadeTo('fast', 0.3); } // don't fade out if the parent is active
				)
			}
		});
	}
}

var IE8bar = {
    init: function() {
        if (parseInt(IEVersion().Version) == 8 && (window.location.hostname == "www.118.lt" || window.location.hostname == "118.lt"))
            if ((!window.external.IsServiceInstalled(acc_url, 'Search')) && ($j.cookie('_118hideIE8bar') != 1)) {
            setTimeout(function() { if ($j('#IE8bar').get(0) != undefined) { $j('#IE8bar').slideDown('2000') } }, "2000");
        }

        $j('#IE8barClose').click(function() {
            $j.cookie('_118hideIE8bar', '1', { domain: '', path: '/', expire: 100 });
            $j('#IE8bar').slideUp('2000')
        });
        $j('#IE8bardownload').click(function() {
            setTimeout(function() {
                if (window.external.IsServiceInstalled(acc_url, 'Search'))
                    $j('#IE8bar').slideUp('2000')
            }, "2000");
        });

    }
}

/*----------- Sliding tabs -----------------*/
var slidingTabs = {
    init: function() {
        $j("DIV.tab-menu A.move-left").click(this.moveLeft);
        $j("DIV.tab-menu A.move-right").click(this.moveRight);
    },
    moveRight: function() {
        if (slidingTabs.getCurrentSlidePosition() < 0) {
            slidingTabs.slideTo(-1028);
            slidingTabs.slideTo(0);
        }
        else {
            slidingTabs.slideTo(-928);
        }

        return false;
    },
    moveLeft: function() {
        if (slidingTabs.getCurrentSlidePosition() < 0) {
            slidingTabs.slideTo(0);
        }
        else {
            slidingTabs.slideTo(100);
            slidingTabs.slideTo(-928);
        }
        return false;
    },
    slideTo: function(pos) {
        $j("DIV.tab-menu DIV.viewport UL.form-tabs").animate({
            marginLeft: pos + "px"
        }, 200);
    },
    getCurrentSlidePosition: function() {
        return parseInt($j("DIV.tab-menu DIV.viewport UL.form-tabs")[0].style.marginLeft);
    }
};

/*------------- sliding ad ---------------*/
var slidingAd = {
	init: function() {
	$j(function() { //on DOM ready
		if ($j("#scroller").get() != undefined)
			$j("#scroller").simplyScroll({autoMode: 'loop'});
		});
	}
}
var slidingAdIe6fix = {
    init: function() {
        if ($j('#ie6fix ').get(0) != undefined) {
            $j('#ie6fix ').css('position', 'absolute');
            $j(window).scroll(function() {
                var h = $j(window).height();
                //var t = h - $j('#ie6fix #scroller').height() + $j(this).scrollTop();
                var t = h + $j(this).scrollTop();
                $j('#ie6fix ').css('top', t + "px");
            });
        }
    }
}

$j(document).ready(function() {
    IE8bar.init();
    slidingAd.init();
    /* IE */
    if ($j.browser.msie) {
    }

    /* IE6 */
    if ($j.browser.msie && ($j.browser.version < 7)) {
        dataHover();
        slidingAdIe6fix.init();
    }

    _118NotepadFrame.init();
    gallery.init();
    slidingTabs.init();
});
